运行导出的程序时出现异常,但不是在Eclipse中

时间:2013-04-11 23:41:56

标签: java eclipse export

我正在编写一个程序,并试图将其导出并发送给某人。当我在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)

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方式:

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

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


或者如果您想使用内置 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项目看起来像

enter image description here

我将此项目导出到带有images文件夹

的JAR

right-click on project - &gt; Export - &gt; JAR file - &gt; next

enter image description here

next - &gt; next - &gt; select main class

enter image description here

我双击创建的JAR文件,并使用我的图片查看框架:

enter image description here