gpt4 book ai didi

java - 尝试设置 Jython 解释器时出错

转载 作者:行者123 更新时间:2023-11-30 09:48:03 25 4
gpt4 key购买 nike

我想在插件中启动一个 Jython 解释器,然后在解释器中执行文件。

package com.jakob.jython;

import org.bukkit.plugin.java.JavaPlugin;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

public class EmbeddedJython extends JavaPlugin {

private static final Logger log = Logger.getLogger("Minecraft");
private PythonInterpreter interpreter;
private PyObject plugin;


public void onEnable() {
log.info("JythonTest enabled");
this.interpreter = new PythonInterpreter();
log.info("Jython interpreter fired up and cooking on gas");

log.info("Loading Python Plugin");

InputStream pythonScript = EmbeddedJython.class.getResourceAsStream("MyPlugin.py");
this.interpreter.execfile(pythonScript, "MyPlugin.py");

log.info("Getting plg method.");
this.plugin = this.interpreter.get("plg");
log.info("Plugin loaded");
log.info(plugin.invoke("onEnable").toString());
}

public void onDisable() {
log.info(plugin.invoke("onDisable").toString());
log.info("Jython disabled");
}

}

在其他测试中,我可以启动 Jython 解释器并运行我传递给它的字符串,但是当我使用上面的代码片段时,我最终得到一个看起来像这样的异常:

11:03:38 [SEVERE] Error occurred while enabling JythonTest v0.12 (Is it up to da
te?): null
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at org.python.core.ParserFacade.adjustForBOM(ParserFacade.java:371)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:298)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:288)
at org.python.core.ParserFacade.parse(ParserFacade.java:183)
at org.python.core.Py.compile_flags(Py.java:1717)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:235
)
at com.jakob.jython.EmbeddedJython.onEnable(EmbeddedJython.java:26)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:125)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
.java:750)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
r.java:253)
at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:132)
at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:110)
at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:218)
at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:205)
at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:145)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:265)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)

java.io.IOException: java.io.IOException: Stream closed

at org.python.core.PyException.fillInStackTrace(PyException.java:70)
at java.lang.Throwable.<init>(Throwable.java:181)
at java.lang.Exception.<init>(Unknown Source)
at java.lang.RuntimeException.<init>(Unknown Source)
at org.python.core.PyException.<init>(PyException.java:46)
at org.python.core.PyException.<init>(PyException.java:43)
at org.python.core.Py.JavaError(Py.java:481)
at org.python.core.ParserFacade.fixParseError(ParserFacade.java:104)
at org.python.core.ParserFacade.parse(ParserFacade.java:186)
at org.python.core.Py.compile_flags(Py.java:1717)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:235
)
at com.jakob.jython.EmbeddedJython.onEnable(EmbeddedJython.java:26)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:125)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
.java:750)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
r.java:253)
at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:132)
at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:110)
at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:218)
at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:205)
at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:145)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:265)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at org.python.core.ParserFacade.adjustForBOM(ParserFacade.java:371)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:298)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:288)
at org.python.core.ParserFacade.parse(ParserFacade.java:183)
... 13 more

会不会是我用错了execfile?或者它是一个错误还是我在 InputStream 的工作方式中遗漏了什么?

最佳答案

您确定 EmbeddedJython.class.getResourceAsStream("MyPlugin.py"); 正在寻找资源吗?

如果找不到资源,getResourceAsStream 将返回一个 null,Jython 解释器可能会将其视为空(已关闭)流.

您的应用程序可能应该检查这一点,而不是假设 getResourceAsStream 已经成功。


If I private File pythonFile = new File('MyScirpt.py'); and then hand getResourceAsStream(pythonFile.getAbsolutePath());

那是行不通的。 getResourceAsStream 方法在类加载器的类路径上查找资源,并期望一个可以相对于类路径上的某个条目解析的路径。

你可能想这样做:

private File pythonFile = new File('MyScirpt.py');
InputStream is = new FileInputStream(pythonFile);

并将is传递给解释器。您还需要安排在解释器完成时流总是关闭......无论发生什么机制。

关于java - 尝试设置 Jython 解释器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6423550/

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