How to include JavaScript file in Java program

2019-05-22 18:51发布

问题:

I have this script file created using phantomjs

var webPage = require('webpage');
var page = webPage.create();

page.open(URL, function (status) {
    var content = page.content;
    console.log('Content: ' + content);
    phantom.exit();
});

now I want to use this script in a Java program but I can't. I do this in my project

ScriptEngineManager s = new ScriptEngineManager();
ScriptEngine se = s.getEngineByName("JavaScript");
se.eval(new FileReader("myScript.js"));

but `this is the result

Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "require" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
at provaJavaScript.Client.main(Client.java:15)
Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "require" is not defined. (<Unknown source>#1)
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3770)
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3748)
at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3833)
at sun.org.mozilla.javascript.internal.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2218)
at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1510)
at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849)
at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:162)
at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:430)
at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116)
at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109)
at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3160)
at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:173)
at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1169)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214)
... 2 more

How can I do?

edit: I put this line in my code:

Process process = Runtime.getRuntime().exec("/usr/bin/phantomjs myScript.js");

but this doesn't work. The progrma returns the same errors as before. what can i do??

回答1:

Require is not a part of standard javascript, but a feature of NodeJS. Your JAVA program doesn´t know how to deal with it.

See also this question and its accepted answer:

What is this Javascript "require"?



回答2:

The scripting engine doesn't have access to objects (require) added to JavaScript by PhantomJS/nodejs

You can execute phantom using exec. Something like

 // You need the correct path for phantomJs
 Process process = Runtime.getRuntime().exec("/usr/bin/phantomjs myScript.js");

See https://stackoverflow.com/a/16891381/227299