aalku
aalku

Reputation: 2878

How to set java-8 Nashorn javascript engine options for a single engine instance

I have read that Nashorn supports some options but it seems all of them are undocumented.

They can be used by setting system properties but they are global. I want to set them programmatically for a single engine instance.

Here you can read it is possible to specify options the way I want but that classes are internal so I get this kind of error:

"Access restriction: The type NashornScriptEngineFactory is not accessible due to restriction on required library C:\Program Files\Java\jdk1.8.0\jre\lib\ext\nashorn.jar".

I can't find a way to set those options with the java scripting API.

EDIT:

It seems that the problem is eclipse protecting me from using internal classes and not exactly a problem using nashorn but I will concrete the question.

Is there a way to setup the options of a single nashorn engine instance without using nashorn classes that are not part of the javax.script API?

Upvotes: 3

Views: 3001

Answers (1)

Mike Clark
Mike Clark

Reputation: 10136

The following program runs successfully for me:

import javax.script.*;
import jdk.nashorn.api.scripting.*;
public class NashTest {
    public static void main(String[] args) throws ScriptException {
        NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        ScriptEngine engine = 
            factory.getScriptEngine(new String[] { "--global-per-engine" });
        engine.eval("java.lang.System.out.println('hello world!')");
    }
}

Make sure you're using a released version of JRE/JDK 8.

Upvotes: 3

Related Questions