Here is my simple java script test.js with class :
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
myCar = new Car("Ford", 2014);
Print("done")
Here is my java code which will try to load test.js
public class Controller {
public static void main(String[] args) {
try {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine("--language=es6");
engine.eval(Files.newBufferedReader(Paths.get("<Path-to-test-js>/test.js"), StandardCharsets.UTF_8));
} catch (Exception _e){
System.out.println(_e);
}
}
but its throwing exception :
javax.script.ScriptException: <eval>:1:0 Expected an operand but found class
class Car {
^ in <eval> at line number 1 at column number 0
Why its throwing exception here.