0

I am using Java 7, and I have the following code that uses the jsondiffpatch Javascript library:

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);

If you look at line 48 of jsondiffpatch.js (the uncompressed version of jsondiffpatch.min.js), you can see it references another module:

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

When executing the Java code, the engine.eval(reader) line can't find package.json and throws the following exception:

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]

The jsondiffpatch.min.js file is located in the webclient/common/js directory, and the package.json file is located in the webclient/common directory.

The logger.debug line printed out the following for the current working directory:

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

So I tried copying the package.json file to the /usr/local/project/version directory, and I still got the same exception.

Why can't the ScriptEngine find the package.json file?

3 Answers 3

1

the jsondiffpatch.js file found in /public/build is a bundle (created with browserify) intended only for using it in a browser (the require function called there is defined at the top of the file, it doesn't read files from disc).

I'm not familiar with Java ScriptEngine, but it looks it's not allowing a redefine of the require function. if it supports CommonJS, you shouldn't use the bundled version, you should require ./src/main.js (the library main module, as pointed in package.json)

(Disclaimer: I'm jsondiffpatch author)

Sign up to request clarification or add additional context in comments.

Comments

0

Attempt to try the full path of the module you want to include.

1 Comment

Do you mean change the var packageInfoModuleName = '../package.json'; line to be var packageInfoModuleName = '/usr/local/project/version/Software/webclient/common/package.json';? That line is in 3rd-party code, and ideally we don't want to change that, otherwise we'd have to change it every time we get a new version of the library. Also, I just tried it, and it didn't work anyway (same exception).
0

I used Java 8, Nashorn, nashorn-commonjs-modules, jsondiffpatch.js.

I deleted chalk external module and package.json from the file main.js because it's outside the scope of my Nashorn base folder.

It's work now :)

    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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.