I have an Eclipse RCP application with the facility to evaluate a JavaScript text on the Nashorn JVM.
Bundle A implements the evaluation of the JS.
The JS script is referencing classes in Bundle B, eg. a enum class. But this Bundle B is not known by Bundle A. And Bundle B loads the classes dynamically by reading an external jar file.
Now when i use importPackage or Java.type i get an ClassNotFoundException.
In Bundle A, i can get retrieve an object from Bundle B from the JAR. With it's classloader i can also access other classes from the JAR.
getBundleBObj().getClass().getClassLoader().loadClass( "my.ClassEnum" );
This succeeds.
Can I use this somehow, to make the package 'my.*' (from Bundle B) be available in the JS?
If you have a java.lang.Class instance, you could expose it as a global variable to script and use "static" property on it.
In Java:
// Class yourClassObject = ...
// expose Class object as variable to your script
// "engine" is nashorn engine instance
engine.put("MyClass", yourClassObject);
In JavaScript:
var M = MyClass.static;
var obj = new M(); // create object
M.func(); // call static method