■ Document original (1) https://docs.oracle.com/javase/jp/6/technotes/guides/scripting/programmer_guide/ ■ Document original (2) https://developer.mozilla.org/ja/docs/Rhino/Scripting_Java
package pkg;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class HelloJavaScript {
public void run() {
System.err.println("hello!");
}
public static void main(String[] args) {
// see
// https://developer.mozilla.org/ja/docs/Rhino/Scripting_Java
// These packages are likely not in the java package, so you'll need to
// prefix the package name with "Packages."
// Get the JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
String script = "" //
+ "var obj = new Packages.pkg.HelloJavaScript();" //
+ "obj.run();" //
;
try {
engine.eval(script);
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
Recommended Posts