问题
I have an Eclipse RCP that interacts with java script. Now with Java 8, nashorn is used and code that depended on org.mozilla.javascript (plug-in org.mozilla.javascript_1.7.2.v201005080400.jar) must be changed to use jdk.nashorn.api.scripting.
But when i try to use this import in Eclipse, it does not see it
import jdk.nashorn.api.scripting.JSObject;
I get the error:
Access restriction: The type 'JSObject' is not API (restriction on required library '<...>\jre\lib\ext\nashorn.jar')
How can I make it visible in the classpath at compile time?
回答1:
jdk.nashorn.scripting is part of Nashorn's exposed API. Please see https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/JSObject.html
It appears to some Eclipse specific issue. I used Netbeans 8.0.2 to compile & run the following application and it compiled and ran as expected:
package javaapplication2;
import javax.script.*;
import jdk.nashorn.api.scripting.*;
public class JavaApplication2 {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
ScriptObjectMirror sobj = (ScriptObjectMirror)e.eval("({ foo: 33 })");
System.out.println(sobj.getMember("foo"));
}
}
来源:https://stackoverflow.com/questions/32655117/how-to-make-jdk-nashorn-api-scripting-jsobject-visible-in-plugin