0

我正在尝试将此应用程序导出到 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

4

1 回答 1

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 应该始终有一个尾随空行(最后一行应该是空白)。

于 2013-07-02T20:43:19.767 回答