Boolean return value after evaluating javaScript code using javax.script.ScriptEngine object
I am using javax.script package. * to evaluate javascript in a Java file. I am using ScriptEngine object to evaluate javaScript code. The javaScript code returns a boolean. I'm not sure how to capture the boolean in a Java file. For example, see
import javax.script.*;
public class Test {
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
String filepath = "../test.js";
engine.eval(new java.io.FileReader(filepath));
}
}
test.js
function test(value) {
if(value==1) {
println("True");
return true;
} else {
println("False");
return false;
}
}
var i=1;
test (i);
I tried the following:
Boolean result = (Boolean) engine.eval(new java.io.FileReader(filepath));
The above line throws a java.lang.NullPointerException error.
Thanks, Sony
+3
source to share