我正在尝试将此应用程序导出到 jar 文件,但是一旦我将应用程序导出为可运行的 jar 文件并使用控制台启动它,它就会出现错误(当我从 eclipse 启动它时它没有这样做
这些是我项目中的文件:
这是控制台的堆栈跟踪:
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