最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Java returns cannont invoke ScriptEngine error - Stack Overflow

programmeradmin6浏览0评论

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");

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");
Share Improve this question asked Nov 7, 2020 at 10:55 abhishekmohan20xxabhishekmohan20xx 431 gold badge1 silver badge5 bronze badges 5
  • 2 JDK 15 or later? It's gone. bugs.openjdk.java/browse/JDK-8236933 – Tom Hawtin - tackline Commented Nov 7, 2020 at 11:36
  • Nope JDK 8 is the version I use, JRE 1.8 SE – abhishekmohan20xx 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 – abhishekmohan20xx Commented Nov 7, 2020 at 16:00
  • From stackoverflow./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. – Tom Hawtin - tackline Commented Nov 7, 2020 at 16:47
  • (As a rule, anything thread local is a bad idea. Inheritably thread local, even more so.) – Tom Hawtin - tackline Commented Nov 7, 2020 at 17:04
Add a ment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论