2017-10-23 31 views
0

Java 8 (1.8.0_102)에서 javascript (ES6) 함수를 실행하려고합니다.NashornscriptEngine을 사용하여 Java 8에서 es6을 실행할 수 없습니다.

여기에 자바 스크립트가 잘려 있습니다.

const myfunc = (args) => { 

    if (!(args.name || args.zip)) 
return 

    const result = {...args} 
    const { name, zip, date } = result 

... 
} 

여기 내 자바 코드

내가 PARAMS을 통과하는 단위 테스트를 작성했다
public static Object processArbitraryJavaScript(String params) 
    { 
     String[] options = new String[] {"--language=es6"}; 
     NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 
     NashornScriptEngine engine = (NashornScriptEngine) factory.getScriptEngine(options); 
     Object result = null; 
     try 
     { 
      engine.eval(new FileReader("sample.js")); 
      Invocable inv = (Invocable) engine; 
      result = inv.invokeFunction("myfunc", params); 
     } 
     catch (ScriptException scriptException) 
     { 
      LOGGER.error(
        "ScriptException encountered trying to write arbitrary JavaScript" 
          + scriptException.toString()); 
     } 

     catch (NoSuchMethodException e) { 
      LOGGER.error(
        "No such Method"); 
     } 
     return result; 
    } 

입니다. 내가 테스트를 실행할 때, 나는이 예외

ScriptException encountered trying to write arbitrary JavaScriptjavax.script.ScriptException: <eval>:2:5 Expected : but found (
    if (!(args.name || args.zip)) 
    ^in <eval> at line number 2 at column number 5 

나는 if 문에서 주석을 얻을 자바 스크립트뿐만 코드 아래 학습과 오류를보고.

ScriptException encountered trying to write arbitrary JavaScriptjavax.script.ScriptException: <eval>:5:8 Expected : but found result 
    const result = {...args} 
     ^in <eval> at line number 5 at column number 8 

다시 더 아래, 나는이 내 ScriptEngine의이 ES6로 스크립트를 읽고 생각하고이 오류를

ScriptException encountered trying to write arbitrary JavaScriptjavax.script.ScriptException: <eval>:6:6 Expected: but found { 
    const { name, zip, date } = result 
    ^in <eval> at line number 6 at column number 6 

를 참조하십시오.

내가 뭘 잘못하고 있니?

(btw, 스크립트는 JS에서 잘 작동합니다.)

답변