Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Platform.runLater messes up my Thread.currentThread().getContextClassLoader()

cshJul 29 2013

Hi,

I have a plugin mechanism in my application. The plugins are defines by an interface and are loaded via URLClassLoader. A plugin can also have resources, loaded via ResourceBundle.getBundle.

Whenever a plugin's method is called, I do this, before it is called:

ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();

Thread.currentThread().setContextClassLoader(pluginClassLoaders.get(pluginClass.cast(proxy)));

// Invoke plugin method

Thread.currentThread().setContextClassLoader(oldLoader);

The plugin methods are called in their own thread, that means, when I want to open a window I need to do it on the JavaFX Application Thread with Platform.runLater.

The problem is, that in the run method of the Platform.runLater, my Thread.currentThread().getContextClassLoader() is not my URLClassLoader but the Application Classloader.

Why I need this?

I don't want the plugin developer to always keep in mind, that he has to pass the correct class loader (of his classes) into the ResourceBundle.getBundle method.

Instead I thought I could rely on Thread.currentThread().getContextClassLoader(), which is set in the main application, to load the correct bundle.

It would work, but not if I load the bundle inside the JavaFX Application Thread (but which makes the most sense for UI development).

So my goal was to have a convenient method like:

String getResource(String bundle, String key)

{

return ResourceBundle.getBundle(bundle).getString(key),  Thread.currentThread().getContextClassLoader());

}

To avoid passing the classloader every time.

Any ideas on this?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2013
Added on Jul 29 2013
0 comments
296 views