2

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?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
fbenoit
  • 3,220
  • 6
  • 21
  • 32
  • According to the [JavaDocs](https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/package-summary.html#package.description), the `jdk.nashorn` packages are not meant to be used directly. Why don't you use the classes form `javax.script`? – Rüdiger Herrmann Sep 19 '15 at 10:11
  • 1
    @RüdigerHerrmann in my understanding javax.script does not provide the needed functionality. I evaluate JS and get JS-Objects back. In Java they are instances of type jdk.nashorn.api.scripting.ScriptObjectMirror. I want to use its methods to examine and retrieve the contained data. javax.script does not have this functionality, or do I miss something? – fbenoit Sep 20 '15 at 20:29
  • I am afraid I am no expert of the script engine. If you are certain that you need the feature and don't mind to tie your code to a specific implemenation then that's the way to go. Can you post the relevant parts of your bundle manifest? You probably have to `Import-Package` the necessary jdk.nashorn packages. – Rüdiger Herrmann Sep 21 '15 at 12:40
  • When trying to add it to the Import-Package, i get `No available bundle exports package 'jdk.nashorn.api.scripting'`. I use this for other packages, like `javax.annotation` and there it is working. Now I just updated to java8 u60, but this did not help either. – fbenoit Sep 21 '15 at 13:47
  • 2
    I found the solution in this answer: [Access restriction: The type 'Application' is not API (restriction on required library rt.jar)][1] [1]: http://stackoverflow.com/questions/25222811/access-restriction-the-type-application-is-not-api-restriction-on-required-l – fbenoit Sep 21 '15 at 14:07
  • If the packages are provided by the JRE you will also have to let the system bundle export them like described here: http://spring.io/blog/2009/01/19/exposing-the-boot-classpath-in-osgi/ But if your solution also works - all the beter. – Rüdiger Herrmann Sep 21 '15 at 14:30
  • downvoted the "marked as duplicate" because the nashorn question is more specific than the other, and requires additional attention – keesp Dec 27 '18 at 13:49

1 Answers1

0

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"));
    }
}
A. Sundararajan
  • 4,277
  • 1
  • 15
  • 30
  • Thanks for your effort. But my question was explicitly about Eclipse plugin, so this is not a solution for my answer. Sure if i use another environment or put jre\lib\ext\nashorn.jar explicitly on the classpath, i can compile. But what is the "good way" in an Eclipse plugin? – fbenoit Sep 21 '15 at 09:01