admin管理员组文章数量:1435859
I have an object in my script, that contains fields and methods. I can call the methods in Java with invokeMethod()
but can't seem to get the content of the fields of the object. I've got this JavaScript code:
var Test = {
TestVar: "SomeTest",
TestFunc: function() {
print("Hello");
}
};
In this Java Class:
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class ScriptTest {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
engine.eval("var Test = { TestVar: \"SomeTest\", TestFunc: function() { print(\"Hello\");}};");
} catch (ScriptException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println(engine.get("Test"));
System.out.println(engine.get("Test.TestVar"));
System.out.println(engine.get("Test[TestVar]"));
System.out.println(engine.get("Test[\"TestVar\"]"));
Invocable inv = (Invocable) engine;
try {
inv.invokeMethod(engine.get("Test"), "TestFunc");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
This gives me the output
[object Object]
null
null
null
Hello
Is there any way I can access the TestVar
variable directly?
I have an object in my script, that contains fields and methods. I can call the methods in Java with invokeMethod()
but can't seem to get the content of the fields of the object. I've got this JavaScript code:
var Test = {
TestVar: "SomeTest",
TestFunc: function() {
print("Hello");
}
};
In this Java Class:
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class ScriptTest {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
engine.eval("var Test = { TestVar: \"SomeTest\", TestFunc: function() { print(\"Hello\");}};");
} catch (ScriptException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println(engine.get("Test"));
System.out.println(engine.get("Test.TestVar"));
System.out.println(engine.get("Test[TestVar]"));
System.out.println(engine.get("Test[\"TestVar\"]"));
Invocable inv = (Invocable) engine;
try {
inv.invokeMethod(engine.get("Test"), "TestFunc");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
This gives me the output
[object Object]
null
null
null
Hello
Is there any way I can access the TestVar
variable directly?
1 Answer
Reset to default 6Either:
engine.eval("Test.TestVar");
or
((JSObject)engine.get("Test")).getMember("TestVar");
should work.
本文标签: javascriptAccess object variable in Java NashornStack Overflow
版权声明:本文标题:javascript - Access object variable in Java Nashorn - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745671695a2669608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论