javascript - Access object variable in Java Nashorn - Stack Overflow
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.
最新文章
- 芯片缺货严重,真凶或是ABF产能不足
- pycharm - Why would the python code throw an exception (invalid instance in App.root), if "@property" is remov
- anova - Conducting a repeated-measures ANCOVA in R -- utterly lost - Stack Overflow
- javascript - Responsiveness textContent (graphic) - Stack Overflow
- google bigquery - How to Load Large Tables Beyond the 10GB Query Limit in Power BI Premium? - Stack Overflow
- amazon web services - How to create a CloudWatch alarm for an EventBridge Pipe's stopped state in AWS? - Stack Overflow
- mvvm - How to Call ViewModelProvider without ref in Flutter - Stack Overflow
- javascript - Mapping through these JSON elements - Stack Overflow
- How to implement GPU memory recycling in CUDA C++ for data streaming in TensorFlow? - Stack Overflow
- python - Steps approximation for time series scatter with mean changing every K number of steps using BIC - Stack Overflow
- oauth 2.0 - how Can we add group roles claim in okta access token or id token - Stack Overflow
- utf 8 - correct way to Import database dump in PowerShell - Stack Overflow
- Meta Graph API Post engagement - Stack Overflow
- assembly - Why could not I apply call gate successfully? - Stack Overflow
- python - Using OpenCV to achieve a top-down view of an image with ArUco Markers - Stack Overflow
- Can't swap camera with AVCaptureMultiCamSession with SwiftAVKit for iOS - Stack Overflow
- java - YubiKey PIV AuthenticationDecryption returns 0x6A80 error - Stack Overflow