java 无法弄清楚为什么ScriptEngine引擎在代码中为null

yhuiod9q  于 2023-04-19  发布在  Java
关注(0)|答案(2)|浏览(668)

这是我为MapleStory服务器运行的一些代码。每当一个事件的脚本(如与NPC交谈)应该发生时,该脚本将被运行以创建一个脚本路径,用于调用任何脚本(NPC,门户,事件等)。
我也在用jdk1.7.0_80

lang-java
package scripting;

import java.io.File; 
import java.io.FileReader; 
import java.io.IOException;
import javax.script.Invocable; 
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import client.MapleClient; 
import tools.FileoutputUtil;

public abstract class AbstractScriptManager {

    private static final ScriptEngineManager sem = new ScriptEngineManager(null);

    protected Invocable getInvocable(String path, MapleClient c) {
        return getInvocable(path, c, false);
    }

    protected Invocable getInvocable(String path, MapleClient c, boolean npc) {
        FileReader fr = null;
        try {
            path = "scripts/" + path;
            ScriptEngine engine = null;

            if (c != null) {
                engine = c.getScriptEngine(path);
            }
            if (engine == null) {
                File scriptFile = new File(path);
                if (!scriptFile.exists()) {
                    return null;
                }
                engine = sem.getEngineByName("JavaScript");
                if (c != null) {
                    c.setScriptEngine(path, engine);
                }
                fr = new FileReader(scriptFile);
                engine.eval(fr);
            } else if (c != null && npc) {
                c.getPlayer().dropMessage(-1, "You already are talking to this NPC. Use @ea if this is 
                not intended.");
            }
            return (Invocable) engine;
        } catch (Exception e) {
            System.err.println("Error executing script. Path: " + path + "\nException " + e);
            FileoutputUtil.log(FileoutputUtil.ScriptEx_Log, "Error executing script. Path: " + path + 
            "\nException " + e);
            return null;
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException ignore) {

            }
        }
    } 
}

这是我收到的bat错误:

执行脚本时出错。路径:scripts/event/someEvent.js异常java.lang.NullPointerException:无法调用“javax.script.ScriptEngine.eval(java.io.Reader)”,因为“engine”为null
每当我尝试与使用此方法的东西交互时(即单击NPC或在运行某些脚本时启动服务器),都会抛出这些错误。

lymgl2op

lymgl2op1#

出现NullPointerException是因为您使用的Java运行时没有安装“JavaScript”脚本引擎,导致sem.getEngineByName("JavaScript")返回null
通过运行以下代码验证了这一点(参见注解):

ScriptEngineManager sem = new ScriptEngineManager();
List<ScriptEngineFactory> factories = sem.getEngineFactories();
for (ScriptEngineFactory factory : factories)
    System.out.println(factory.getEngineName() + " " + factory.getEngineVersion() + " " + factory.getNames());
if (factories.isEmpty())
    System.out.println("No Script Engines found");

当我在Windows 10上的各种Java实现上运行这个时,我得到以下结果:

OpenJDK jdk1.7.0_75:
  No Script Engines found

Oracle jdk1.7.0_80:
  Mozilla Rhino 1.7 release 3 PRERELEASE [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]

Oracle jdk1.8.0_181:
  Oracle Nashorn 1.8.0_181 [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]

Oracle jdk-9.0.4:
  Oracle Nashorn 9.0.4 [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]

OpenJDK jdk-11.0.2:
  Oracle Nashorn 11.0.2 [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]

AdoptOpenJDK jdk-14.0.2+12:
  Oracle Nashorn 14.0.2 [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]

AdoptOpenJDK jdk-15.0.1+9:
  No Script Engines found

正如您所看到的,Java 7的OpenJDK版本没有JavaScript引擎,因为Mozilla Rhino库不是开源的。您需要Java 7的Oracle版本才能获得JavaScript引擎。
您还可以看到Java 15中删除了JavaScript,如JDK 15发行说明中所述:
Nashorn JavaScript脚本引擎、其API和jjs工具已被删除。在Java 11中不推荐删除该引擎、API和工具,并明确表示将在未来版本中删除它们。请参阅JDK-8236933

6uxekuva

6uxekuva2#

我在使用JDK 17和lib rhino.js.engine-1.7.71.jar时遇到了同样的问题。
我补充道

org.mozilla.javascript-1.7.5.jar

到我的类路径。这解决了错误。

相关问题