4

I am running Java's ScriptEngine class to run to some code. I use IntelliJ with maven dependencies when I run the code it says

Cannot invoke "javax.script.ScriptEngine.eval(String)" because "this.engine" is null
    at Run.Evaluater.<init>(Evaluater.java:19)

the code at line being the following

engine.put("event", event);
engine.put("content", message);

This is how I initiated the ScriptEngone

    public ScriptEngineManager man = new ScriptEngineManager();
    public ScriptEngine engine = man.getEngineByName("nashorn");
5
  • 2
    JDK 15 or later? It's gone. bugs.openjdk.java.net/browse/JDK-8236933 Commented Nov 7, 2020 at 11:36
  • Nope JDK 8 is the version I use, JRE 1.8 SE Commented Nov 7, 2020 at 11:55
  • Even then it says this.engine might be null. That's why I sent the place where instantiated the variable, so someone would be able to say if instantiation was wrong Commented Nov 7, 2020 at 16:00
  • From stackoverflow.com/questions/25332640/… it looks like the no-args ScriptEngineManager constructor uses Thread.currentThread().getContextClassLoader(). Specifying null as an argument bypasses anything weird the tccl might be doing, but still depends upon nashorn being present. Commented Nov 7, 2020 at 16:47
  • (As a rule, anything thread local is a bad idea. Inheritably thread local, even more so.) Commented Nov 7, 2020 at 17:04

2 Answers 2

1

This error was encountered when a project was cloned from the code cloud when it was running. This part of the content is about the loading of the verification code. I did not understand the reason but it was a version problem. Change the JDK version from The problem is solved when 13 is reduced to 8.

Sign up to request clarification or add additional context in comments.

1 Comment

Change to openjdk 8, worked for me. Thanks!
1

Since Java JDK 15, the nashorn engine has been removed. Then trying to use this engine launch the described exception on the question.

You can add it as an external dependency. For example, using maven:

<dependency>
  <groupId>org.openjdk.nashorn</groupId>
  <artifactId>nashorn-core</artifactId>
  <version>15.1</version>
</dependency>

This explains why returning to JDK 8, fix the issue, as JDK 8 has the nashorn engine embedded.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.