Java ScriptEngine找不到Javascript文件引用的模块

时间:2015-05-27 15:37:07

标签: java javascript scriptengine

我使用的是Java 7,我有以下使用jsondiffpatch Javascript库的代码:

logger.debug("CURRENT PATH=" + Paths.get("").toAbsolutePath());
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
reader = new FileReader("webclient/common/js/jsondiffpatch.min.js");
engine.eval(reader);

如果查看jsondiffpatch.js的第48行(jsondiffpatch.min.js的未压缩版本),您可以看到它引用了另一个模块:

var packageInfoModuleName = '../package.json';

执行Java代码时,engine.eval(reader)行无法找到package.json并抛出以下异常:

javax.script.ScriptException: Error: Cannot find module '../package.json' in <Unknown source> at line number 1
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224) ~[na:1.7.0_51]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249) ~[na:1.7.0_51]
    at nms.server.domain.AuditManager$ConfigAuditProcessor.run(AuditManager.java:465) ~[nms.jar:na]
Caused by: sun.org.mozilla.javascript.internal.JavaScriptException: Error: Cannot find module '../package.json' (<Unknown source>#1)
    at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1066) ~[na:1.7.0_51]
    at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849) ~[na:1.7.0_51]
    at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:162) ~[na:1.7.0_51]
    at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:430) ~[na:1.7.0_51]
    at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116) ~[na:1.7.0_51]
    at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109) ~[na:1.7.0_51]
    at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3160) ~[na:1.7.0_51]
    at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:173) ~[na:1.7.0_51]
    at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1169) ~[na:1.7.0_51]
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214) ~[na:1.7.0_51]

jsondiffpatch.min.js文件位于webclient/common/js目录中,package.json文件位于webclient/common目录中。

logger.debug行打印出以下当前工作目录:

 CURRENT PATH=/usr/local/project/version/Software

所以我尝试将package.json文件复制到/usr/local/project/version目录,但仍然有相同的例外。

为什么ScriptEngine无法找到package.json文件?

3 个答案:

答案 0 :(得分:1)

在/ public / build中找到的jsondiffpatch.js文件是一个bundle(用browserify创建),仅用于在浏览器中使用它(在那里调用的require函数定义在文件的顶部,它不会从光盘读取文件。

我不熟悉Java ScriptEngine,但它看起来不允许重新定义require函数。如果它支持CommonJS,你不应该使用捆绑版本,你应该需要./src/main.js(库主模块,如package.json所示)

(免责声明:我是jsondiffpatch作者)

答案 1 :(得分:0)

尝试尝试要包含的模块的完整路径

答案 2 :(得分:0)

我使用了Java 8,Nashorn,nashorn-commonjs-modules,jsondiffpatch.js。

我从main.js文件中删除了chalk外部模块和package.json,因为它超出了我的Nashorn基本文件夹的范围。

现在正在工作:)

    public String diff() throws ScriptException {

    NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
    FilesystemFolder rootFolder = FilesystemFolder.create(new File("C:\\Users\\Maxime\\Documents\\writecontrol\\default\\gateway\\src\\main\\resources\\static\\bower_components\\jsondiffpatch\\src"), "UTF-8");
    Module module = Require.enable(engine, rootFolder);

    try {
        ScriptObjectMirror result = (ScriptObjectMirror)  engine.eval("var jsondiffpatch = require('./main.js');" +
                " var country = {\n" +
                "                name: \"Argentina\",\n" +
                "                capital: \"Buenos Aires\",\n" +
                "                independence: new Date(1816, 6, 9),\n" +
                "                unasur: true\n" +
                "    };" +
                "var country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver);" +
                "    country2.name = \"Republica Argentina\";\n" +
                "    country2.population = 41324992;\n" +
                "    delete country2.capital;\n" +
                "    var delta = jsondiffpatch.diff(country, country2);" +
                "print(delta.name);" +
                "print('Nashorn Hello World!');" +
                "data=JSON.stringify(delta);" +
                "var result = {delta : data};" +
                "result");

        delta = (String) result.getMember("delta");


        System.out.println(delta);

    } catch (final ScriptException se) {
        se.printStackTrace();
    }

    return delta;
}