我有一个内存类加载器 ( here ),我正在自定义 Minecraft 启动器中使用它。
每当我加载 Minecraft(一款 Java LWJGL 游戏)时,都会收到以下错误:
27 achievements
182 recipes
Setting user
LWJGL Version: 2.4.2
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at lc.<init>(SourceFile:21)
at gi.<init>(SourceFile:10)
at net.minecraft.client.Minecraft.a(SourceFile:254)
at net.minecraft.client.Minecraft.run(SourceFile:657)
at java.lang.Thread.run(Unknown Source)
我正在创建这样的类加载器:
Base.cLoader = new CLoader(
GameUpdater.classLoader,
new JarInputStream(new ByteArrayInputStream(jarFileBytes)));
如您所见,它成功加载了第一部分,然后在 LWJGL 版本之后突然崩溃并显示“input == null”。
编辑 - 这是新的 getResource 方法。
错误出现在“URL()”上,如图所示。
代码:
public URL getResource(final String name) {
URL url = new URL() { public InputStream openStream() {
return new ByteArrayInputStream((byte[])others.get(name));
}};
return url;
}
最佳答案
一个疯狂的猜测...可能是这样的:警告:此 URL 尚未实现!您不能调用 getResource() 或 getResources()!
因此您的代码期望使用未实现的方法从 JAR 中检索图像。可能正在执行与此等效的操作:
ImageIO.read(memClassLoader.getResource(someString));
除此之外,正如我们所见,从 getResource
抛出的错误被忽略,并且 null
被用作值。 ImageIO.read
像这样:
public static BufferedImage read(URL input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("input == null!");
}
InputStream istream = null;
try {
istream = input.openStream();
} catch (IOException e) {
throw new IIOException("Can't get input stream from URL!", e);
}
}
听起来很熟悉吗?因此,这大致就是您需要实现的内容:
public URL getResource(final String name) {
try {
return new URL("", "", 0, "",
new URLStreamHandler() { public URLConnection openConnection(URL url) {
return new URLConnection(url) {
public void connect() {}
public InputStream getInputStream() {
// return the appropriate InputStream, based on the name arg
}
};
}});
} catch (MalformedURLException e) { throw new RuntimeException(e); }
}
关于Java MemoryClassLoader(IllegalArgumentException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10124541/