Java Reference
In-Depth Information
// Use global and engine scopes bindings. num1 from
// engine scope and num2 from global scope will be used.
engine.put("num1", 70.0);
script = "num1 + num2";
result = engine.eval(script);
System.out.println(script + " = " + result);
// Try mixture of number literal and bindings. num1
// from the engine scope bindings will be used
script = "10 + num1";
result = engine.eval(script);
System.out.println(script + " = " + result);
}
catch (ScriptException e) {
e.printStackTrace();
}
}
public static void testReader(ScriptEngineManager manager,
ScriptEngine engine) {
try {
Path scriptPath = Paths.get("jkscript.txt").
toAbsolutePath();
if (!Files.exists(scriptPath)) {
System.out.println(scriptPath +
" script file does not exist.");
return;
}
try(Reader reader = Files.
newBufferedReader(scriptPath);) {
Object result = engine.eval(reader);
System.out.println("Result of " +
scriptPath + " = " + result);
}
}
catch(ScriptException | IOException e) {
e.printStackTrace();
}
}
}
Search WWH ::




Custom Search