gpt4 book ai didi

java - 执行jar文件时出现IIOException

转载 作者:行者123 更新时间:2023-12-01 14:22:23 25 4
gpt4 key购买 nike

我正在尝试将此应用程序导出到 jar 文件,但是一旦我将应用程序导出为可运行的 jar 文件并使用控制台启动它,就会出现错误(当我从 eclipse 启动它时,它没有这样做

这些是我的项目中的文件:

enter image description here

这是控制台的堆栈跟踪:

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at john.z.moohrhuhn.MoohuhnGUI.initialize(MoohuhnGUI.java:57)
at john.z.moohrhuhn.MoohuhnGUI.<init>(MoohuhnGUI.java:40)
at john.z.moohrhuhn.Main.main(Main.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.NullPointerException
at sun.awt.CustomCursor.<init>(Unknown Source)
at sun.awt.windows.WCustomCursor.<init>(Unknown Source)
at sun.awt.windows.WToolkit.createCustomCursor(Unknown Source)
at john.z.moohrhuhn.MoohuhnGUI.initialize(MoohuhnGUI.java:63)
at john.z.moohrhuhn.MoohuhnGUI.<init>(MoohuhnGUI.java:40)
at john.z.moohrhuhn.Main.main(Main.java:31)
... 5 more

这是 MoohuhnGUI 中的代码(从第 48 行开始):

System.out.println("Path: " + getClass().getResource("Cursor.png").toString());
frame.setBounds(100, 100, 600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit();

BufferedImage img = null;
try {
File f = new File(getClass().getResource("Cursor.png").getFile());
img = ImageIO.read(f);
} catch (IOException e) {
System.out.println("problem");
e.printStackTrace();
}
Dimension bestsize = toolkit.getBestCursorSize(300, 300);
Cursor c = toolkit.createCustomCursor(
img,
new Point((int) bestsize.getHeight() / 2, (int) bestsize
.getHeight() / 2), "testimg");
frame.setCursor(c);
moorhuhn = MoorhuhnFrame.getinstance();
moorhuhn.init();

frame.add(moorhuhn);

我会很高兴你们能帮助我;D

最佳答案

getClass().getResource("Cursor.png") 

期望 Cursor.png 位于类路径中。当您将项目导出为 jar 时,请确保将文件 Cursor.png 复制到类路径中。并在manifest.mf文件中添加类路径引用:

Class-Path: resources

因此,您的目标结构可能是(例如):

moohuhn
|
+--moohuhn.jar
|
+--resources
|
+--Cursor.png

manifest.mf 会是我:

Main-Class: john.z.MoohuhnGUI
Class-Path: resources

请记住,manifest.mf 应始终有一个尾随空白行(最后一行应为空白)。

关于java - 执行jar文件时出现IIOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17435361/

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