我希望能够将对象传递到ScriptEngine
viaput()
并能够从脚本引擎中访问其属性和方法。
例如
public class MyClass {
String getName() { return "abc"; }
}
MyClass my = new MyClass();
engine.put("abc", my);
我怎样才能做到这一点?
我希望能够将对象传递到ScriptEngine
viaput()
并能够从脚本引擎中访问其属性和方法。
例如
public class MyClass {
String getName() { return "abc"; }
}
MyClass my = new MyClass();
engine.put("abc", my);
我怎样才能做到这一点?
这是一个完整的工作示例JavaScript
。正如我在评论中提到的,您必须确保您的方法是public
.
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JavaScriptEngineSample {
public static class MyClass {
private String name;
public String getName() { return name; }
public void setName(final String name) { this.name = name; }
}
public static void main(final String[] args) throws ScriptException {
final MyClass my = new MyClass();
my.setName("Input");
final ScriptEngineManager factory = new ScriptEngineManager();
// you could also use 'javascript' here, I'm using 'nashorn' to visualize that I'm using the new Java 8 Engine
final ScriptEngine engine = factory.getEngineByName("nashorn");
engine.put("my", my);
String script = "java.lang.System.out.println(my.getName());";
script += "my.setName('Output');";
engine.eval(script);
System.out.println(my.getName());
}
}