7

How can I use JavaFX 2.2 in a OSGi bundle? I'm using Apache Felix so it's no eclispe project. It must run on Mac OSX with Java7 installed (Java6 would be cool but not necessary).

I've read something about repacking the jfx libraries but for eclispe plugin projects.

Just importing the javafx packages in the bundle Manifest created a missing requirement osgi.wiring.package Exception.

Christof Aenderl
  • 4,233
  • 3
  • 37
  • 48

3 Answers3

11

It's a little late to help you, probably, but I would like to document a straightforward way to enable JavaFX inside an OSGi environment in case anybody, like me, still had trouble doing it....

The thing is, you need to allow JavaFX the possibility to load and use any class it wants (JavaFX makes use of internals of the JRE which makes it really hard to bundle), so what you really want is to give it access to the OSGi boostraping classpath...

I'm no expert, but in this blog, Costin Leau, from SpringSource, explains how to do it (for any jars which assume full access to the system classloader, not only JavaFX) in 3 different ways:

http://blog.springsource.org/2009/01/19/exposing-the-boot-classpath-in-osgi/

I chose to use option A, which basically means setting the OSGi-specified property (not specific to any implementation!) in your OSGi container's config file:

org.osgi.framework.system.packages.extra=javafx.application;version=0.0.0 ...

You can see the whole value of the property in this Gist I've created:

https://gist.github.com/renatoathaydes/5021107

I basically set it with all of the packages which the JavaFX 2.2 jar can export (and I found this out by simply dropping the JavaFX jar into the deploy folder of my Apache Karaf, which automagically makes it into a bundle, and then looking at the generated Manifest)... by the way, I tried to use the bundle generated by Karaf (which uses pax-wrap to do it) but this won't work due to the way JavaFX tries to load your classes (which are not visible in the attempted JavaFX bundle, not to mention all the JRE packages JavaFX tries to access).

I suspect not all of the packages I added are actually required, but to be safe, I left them all as generated by Karaf's wrapper and as it works, I won't change that unless there's a good reason for that.

Now any of my bundles can use JavaFX as long as the jfxrt.jar is present in the JRE lib folder.

Renato
  • 12,940
  • 3
  • 54
  • 85
  • The version is declared as 0.0.0 because packages coming from outside OSGi are not versioned, so you just give it an 'empty' version number. – Renato Feb 17 '13 at 22:08
7

Try using the e(fx)clipse runtime tools, which include libraries to allow JavaFX to work well in an OSGi environment.

According to the e(fx)clipse site:

Unfortunately JavaFX is not written with OSGi in mind so there are various places which are causing problems when running inside OSGi ranging from the fact JavaFX-Binaries are located somewhere on the users system, to problems when it comes to class loading because of OSGi's visibility rules. e(fx)clipse provides helper libraries to deal with all those problems and makes writing JavaFX applications ontop of Eclipse Equinox a feel as easy as it is with SWT and Swing

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 1
    But I'm not using SWT and eclipse equinox. Isn't there a more generic way of creating such OSGi helper libraries? – Christof Aenderl Nov 06 '12 at 10:55
  • 1
    I advise asking your questions on the [e(fx)clipse forum](https://groups.google.com/forum/m/?fromgroups#!forum/efxclipse) – jewelsea Nov 06 '12 at 17:22
  • 1
    JavaFX in a OSGi Bundle is still a bit tricky. The e(fx)clipse plugin is working with equinox but I didn't get it work with apache Felix so I had to add the javafx classes to boot-classloader not the osgi-bundle. Hope further FX versions will improve here. – Christof Aenderl Nov 29 '12 at 10:57
6

Not a bundle with JavaFX but adding the javafx classes to the boot-classloader would work. Example with Apache Felix:

Map<String,String> properties = new HashMap<String, String>();
properties.put(Constants.FRAMEWORK_BOOTDELEGATION, "javafx.*,com.sun.javafx.*");
properties.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_APP);

FrameworkFactory factory = new org.apache.felix.framework.FrameworkFactory();
org.osgi.framework.launch.Framework framework = factory.newFramework(properties);
framework.init();

Now just use the javafx classes from any bundle (if JavaFX is installed on the client computer).

Christof Aenderl
  • 4,233
  • 3
  • 37
  • 48
  • This is a good approach. Can this be done by only needing to modify your OSGI containers config and not needing extra code? – Mike Pone Mar 31 '14 at 22:42
  • Can someone with Apache Karaf try modifying the jre.properties file in their /etc directory? – Mike Pone Mar 31 '14 at 23:05