Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not run #1

Open
ipeychev opened this issue May 5, 2015 · 5 comments
Open

Does not run #1

ipeychev opened this issue May 5, 2015 · 5 comments
Assignees
Labels

Comments

@ipeychev
Copy link

ipeychev commented May 5, 2015

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?

@ynsn
Copy link
Owner

ynsn commented May 5, 2015

Hello,

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

@ipeychev
Copy link
Author

ipeychev commented May 6, 2015

Thanks!

@ynsn ynsn added the bug label May 6, 2015
@ynsn ynsn self-assigned this May 6, 2015
@ipeychev
Copy link
Author

ipeychev commented May 6, 2015

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

@ipeychev
Copy link
Author

Hey man, any update here?

Is there some serious issue?

@ynsn
Copy link
Owner

ynsn commented May 11, 2015

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants