Skip to main content

Using javafx from a URLClassLoader?

1 reply [Last post]
xastor
Offline
Joined: 2006-08-03
Points: 0

I'm a java developer and the application I'm currently working on has the ability to load extensions. This week I've been trying to make a scripting extension allowing the user to load javafx scripts and execute them.

The first problem I encountered was that the javafx engine was not available through the jsr-223 ScriptEngineManager. The reason for this seemed to be that the application loads up extra jars using a URLClassLoader. When I specified the correct classloader, the manager was able to find the javafx scripting engine.

The next problem which I haven't been able to solve is that the javafx library (all the .fx files in the javafxrt.jar) don't seem to be available! When executing the simple HelloWorld.fx script, I get the message that "Frame" is undefined. I was wondering if anyone here could give me some insight on this?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
xastor
Offline
Joined: 2006-08-03
Points: 0

Ok, I seem to have solved by own problem..

It seems JavaFX loads up the default library files using the "context" classloader which by default seems to be the system classloader.
When JavaFX is loaded by another classloader, we need to setup this classloader as "context" classloader :

ClassLoader oldCCL = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(this.explorer.getClass().getClassLoader());
// use the script engine..
} finally {
Thread.currentThread().setContextClassLoader(oldCCL);
}