Search code examples
eclipse-plugineclipse-rcprhinoeclipse-pde

Scripting Eclipse with Rhino: classloader belongs to the plugin providing Rhino, not the plugin using it


I am using Rhino to script an Eclipse (RCP) application. The problem is that from Javascript I only have access to classes available to the plugin that provides Rhino, and not to all the classes available to the plugin that runs the scripts.

The obvious answer would be to put Rhino in the scripting plugin, but this doesn't work because it's already provided by one of the application's own plugins (which also provides things I need to script) and Eclipse always uses this version instead of the version closer to hand.

  • Is there a way to change the classloader used by Rhino
  • or is it possible to ensure that Eclipse loads the Rhino classes from one plugin rather than another?

Thanks to Thilo's answer I used this:

import net.weissmann.tom.rhino.Activator;  // Plugin activator class
import org.mozilla.javascript.tools.shell.Main;

public class JSServer extends Thread {

    //[...]

    public void run() {
        // recent versions of the Main class kindly export
    // the context factory
        Main.shellContextFactory.initApplicationClassLoader(
                Activator.class.getClassLoader()    
            ) ;

        //[...]
    }

Solution

  • Is there a way to change the classloader used by Rhino

    Rhino should be using the current Thread's ContextClassLoader. Try Thread.setContextClassLoader (don't forget to restore it).

    If that does not work, maybe you can create your own Rhino ContextFactory:

    public final void initApplicationClassLoader(java.lang.ClassLoader loader)

    Set explicit class loader to use when searching for Java classes.