Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does getEngineByName(“js”) return null?

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:

  • https://stackoverflow.com/a/25332851/900394
  • https://stackoverflow.com/a/25332851/900394

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?

like image 449
Alaa M. Avatar asked Feb 11 '23 06:02

Alaa M.


1 Answers

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

getEngineByName("JavaScript");
                 ^   ^
like image 73
Siddharth Kumar Avatar answered Feb 12 '23 21:02

Siddharth Kumar