Why does getEngineByName(“js”) return null?
Asked Answered
K

4

7

Whatever I try, getEngineByName() keeps returning null.

Here's my code:

final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("js");

But engine is null after these lines.

I also tried:

  • getEngineByName("javascript")
  • getEngineByName("nashorn")

They all return null. Actually, manager.getEngineFactories() shows an empty array - meaning there are no Factories at all.

These 2 answers suggest passing null to the constructor, but it didn't work for me:

And this answer says it's a bug that has been fixed.


Update:

That was an Android Application Project in eclipse.

I didn't know it differs that much from a Java Project.

Now I just opened a new Java Project, wrote these lines, and I'm getting some results:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

public class TestClass {

    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = scriptEngineManager.getEngineByName("js");
    }
}

engine isn't null !

Also, javax.script.* was imported successfully as if it's already there (built in). Is this because I'm using Java 8?

In that previous (android) project I was using javax.script downloaded from here.

So what would be the problem in the Android Project and how do I solve it?

Kampong answered 2/5, 2015 at 21:39 Comment(6)
https://mcmap.net/q/1189041/-is-osgi-fundamentally-incompatible-with-jsr-223-scripting-language-discovery See if that helpsAkeyla
I have had success with getEngineByExtension("js")Avidity
@AndrewThompson, Same problem...Kampong
@AlaaM. Any update on this issue?Ssm
@ShailendraMadda - Nope, I dropped the project.Kampong
@AlaaM. I tried this it's working now. val jsEngine = ScriptEngineManager().getEngineByName("rhino") refer this linkSsm
A
5

You have to write the argument with Capital Letters, as shown below:

getEngineByName("JavaScript");
                 ^   ^
Agenesis answered 13/10, 2015 at 6:40 Comment(1)
Even I did the same but still it is nullSsm
H
3

Apparently when running in an IDE even if you are on a recent version of Java that includes the Nashorn javascript engine you have to pass null into the ScriptEngineManager constructor or else it often won't have engines. It'd be fine when actually compiled, seems to be an IDE error. For me it was resolved by changing this code:

mgr = new ScriptEngineManager();
engine = mgr.getEngineByName("JavaScript");

to this code:

mgr = new ScriptEngineManager(null);
engine = mgr.getEngineByName("JavaScript");

Note the null passed into the ScriptEngineManager constructor call.

Habituate answered 13/3, 2019 at 18:35 Comment(2)
Tried but still facing the same issueSsm
I tried this it's working now. val jsEngine = ScriptEngineManager().getEngineByName("rhino") refer this linkSsm
P
2

Your version of Java does not include a JavaScript engine. Java 8 includes the Nashorn javascript engine and has in general better support for Java <-> JavaScript interoperability. For older versions of Java you can put Rhino on the classpath and use that.

Palmy answered 11/1, 2016 at 14:10 Comment(0)
S
0

How about adding the module jdk.scripting.nashorn to the module graph?

java --add-modules jdk.scripting.nashorn
Semester answered 27/8, 2021 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.