我正在尝试将此应用程序导出到jar文件但是一旦我将我的应用程序导出为可运行的jar文件并使用控制台启动它就会出错(当我从eclipse启动时它没有这样做< / p>
这些是我项目中的文件:
这是控制台的堆栈跟踪:
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
答案 0 :(得分:1)
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应始终有一个尾随空白行(最后一行应为空白)。