本文介紹了我找不出為什么ScriptEngine引擎在代碼中為空的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
這是我為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 + "
Exception " + e);
FileoutputUtil.log(FileoutputUtil.ScriptEx_Log, "Error executing script. Path: " + path +
"
Exception " + e);
return null;
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException ignore) {
}
}
}
}
這是我收到的BAT錯誤:
執行腳本時出錯。路徑:腳本/事件/某些Event.js異常
Java.lang.NullPointerException:無法調用
";javax.script.ScriptEngine.eval(java.io.Reader)";,因為引擎為空
每當我嘗試與使用此方法的對象交互時(例如,在運行某些腳本時單擊NPC或啟動服務器),都會引發這些錯誤。
JAVA
出現NullPointerException
是因為您使用的推薦答案運行時沒有安裝腳本引擎,導致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版本沒有Java引擎,因為Mozilla Rhino庫不是開源的。您需要安裝Oracle版本的Java 7才能獲得Java引擎。
您還可以看到Java 15中刪除了JavaScript,如JDK 15 Release Notes:
中所述
已刪除Nashorn腳本引擎、其API和
jjs
工具。不建議在Java 11中刪除引擎、API和工具,并明確希望在將來的版本中刪除它們。請參閱jdk-8236933
這篇關于我找不出為什么ScriptEngine引擎在代碼中為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,