nashorn icon indicating copy to clipboard operation
nashorn copied to clipboard

Does not run

Open ipeychev opened this issue 10 years ago • 5 comments

Hello,

Could you please help with the following issue - how exactly am I supposed to use Nashorn from Java 7?

I have the following simple code:

import javax.script.*;
import java.util.*;

public class Nashorn {
    public static void main(String[] args) {
        ScriptEngineManager manager = new ScriptEngineManager ();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();

        for (ScriptEngineFactory factory : factories) {
            System.out.println(factory.getEngineName());
        }

        ScriptEngine engine = manager.getEngineByName ("nashorn");
        String script = "print ('www.java2s.com')";
        try {
            engine.eval (script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

I compile it: $ javac Nashorn.java

and then I run it: $ java -cp nashorn-1.0.jar:. Nashorn

nashorn-1.0.jar is in the current folder.

However, there is exception: Exception in thread "main" java.lang.NullPointerException at Nashorn.main(Nashorn.java:18) This means the "engine" is null. And yes, I have only "Mozilla Rhino" registered, but not Nashorn. Where is the mistake, please?

ipeychev avatar May 05 '15 19:05 ipeychev

Hello,

The Nashorn library seems, indeed, that it doesn't work. I'll look into it.

ynsn avatar May 05 '15 22:05 ynsn

Thanks!

ipeychev avatar May 06 '15 08:05 ipeychev

Btw, are there some performance penalties in comparison with Nashorn run in Java 8?

ipeychev avatar May 06 '15 10:05 ipeychev

Hey man, any update here?

Is there some serious issue?

ipeychev avatar May 11 '15 14:05 ipeychev

Sorry for the delay. I don't have a solution for now, but I have an update.

The reason a NullPointerException is occuring, is that the NashornScriptEngineFactory is not registered with the ScriptEngineManager, which means that we cannot get the Nashorn scripting engine.

The way a scripting engine must be registered is by providing a META-INF/services/javax.script.ScriptEngineFactory service file containing the full classname of the NashornScriptEngineFactory, which is currently not provided in the archive. I'll try to fix this as soon as possible.

Regarding your question about the performance penalties: since this is a backport, the performance will not likely to be exactly the same as the version provided in Java 8. However, it should still be faster than Rhino after it has warmed up (running at least a little script).

ynsn avatar May 11 '15 15:05 ynsn