0

我正在编写一个程序并尝试将其导出并将其发送给某人。当我在 Eclipse 中运行该程序时,它运行良好。但是,当我将它导出为 Runnable JAR 文件并双击它时,它会退出并告诉我检查控制台。

这是堆栈跟踪:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at me.pogostick29.audiorpg.util.PrereleaseChecker.run(PrereleaseChecker.java:29)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at me.pogostick29.audiorpg.window.Window.<init>(Window.java:62)
    at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

我认为它没有正确导出图像。这是我访问图像的方式:

BufferedImage myPicture = null;
try { myPicture = ImageIO.read(new File("images/audiorpglogo.png")); }
catch (Exception e) { e.printStackTrace(); }
logo = new JLabel(new ImageIcon(myPicture));

另外,当我尝试

myPicture = ImageIO.read(Window.class.getResource("/images/audiorpglogo.png"));

即使在 Eclipse 中运行,我也会得到堆栈跟踪:

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1362)
    at me.pogostick29.audiorpg.window.Window.<init>(Window.java:70)
    at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:29)
4

1 回答 1

0

你可以这样试试:

ImageIcon icon = new ImageIcon("images/myimage.jpg");

images/myimage.jpg如果您在使用 JAR 文件运行应用程序的同一文件夹中有目录,这将起作用。


或者,如果您想使用放置JAR 文件中的图像,您可以使用

ImageIcon icon = new ImageIcon(getClass().getResource("images/myimage.jpg"));

当它在 IDE 中不起作用时不要害怕。当您从 JAR 文件运行它时,它将起作用。


这是一些对我有用的代码示例:

import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyFrame extends JFrame {
    private static final long serialVersionUID = 1L;

    private JLabel iconLabel;

    public MyFrame() throws IOException {

        ImageIcon icon = new ImageIcon(getClass().getResource(
                "images/myimage.jpg"));
        iconLabel = new JLabel(icon);
        getContentPane().add(iconLabel);

        setSize(icon.getIconWidth(), icon.getIconHeight());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        new MyFrame();
    }
}

当我尝试在 Eclipse 中运行它时,我得到

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at MyFrame.<init>(MyFrame.java:14)
    at MyFrame.main(MyFrame.java:25)

这个 Eclipse 项目看起来像

在此处输入图像描述

在我将此项目导出到带有images文件夹的JAR 之后

right-click on project-> Export-> JAR file->next

在此处输入图像描述

next-> next->select main class

在此处输入图像描述

我双击创建的 JAR 文件并查看带有我的图像的框架:

在此处输入图像描述

于 2013-04-12T00:19:42.280 回答