Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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?

share|improve this question

2 Answers 2

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

share|improve this answer
    
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). – pacoverflow May 29 at 20:47

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.