gpt4 book ai didi

javascript - 无法弄清楚为什么 ScriptEngine 引擎在代码中为空

转载 作者:行者123 更新时间:2023-12-05 09:35:37 48 4
gpt4 key购买 nike

这是我为冒险岛服务器运行的一些代码。每当与 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 错误:

Error executing script. Path: scripts/event/someEvent.js Exceptionjava.lang.NullPointerException: Cannot invoke"javax.script.ScriptEngine.eval(java.io.Reader)" because "engine" is null

每当我尝试与使用此方法的东西交互时(即单击 NPC 或在运行某些脚本时在服务器启动时),就会抛出这些错误。

最佳答案

NullPointerException 的发生是因为您使用的 Java 运行时没有安装“JavaScript”脚本引擎,导致 sem.getEngineByName("JavaScript") 到返回 null

已通过运行以下代码验证(参见 comment):

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 Release Notes 中所述:

The Nashorn JavaScript script engine, its APIs, and the jjs tool have been removed. The engine, the APIs, and the tool were deprecated for removal in Java 11 with the express intent to remove them in a future release. See JDK-8236933

关于javascript - 无法弄清楚为什么 ScriptEngine 引擎在代码中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65747123/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com