【问题标题】:Exception When Running Exported Program, but not in Eclipse运行导出程序时出现异常,但在 Eclipse 中没有
【发布时间】:2013-04-12 08:54:37
【问题描述】:

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

【问题讨论】:

  • 转到命令提示符/终端,并使用java -jar 语法运行程序。然后就可以看到异常输出了。在此处发布。
  • 向我们展示异常/跟踪如何?
  • 好吧,很好的堆栈跟踪。不幸的是,它没有告诉我们您的代码。
  • 它说它无法加载图像。我猜 Eclipse 没有将图像添加到 jar 文件中。 javax.imageio.ImageIO.read
  • @PogoStick29 如何访问图像文件?你能告诉我们那个代码吗?

标签: java eclipse export


【解决方案1】:

你可以试试这个方法:

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 项目看起来像

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

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

next -> next -> select main class

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

【讨论】:

  • 我需要 JAR 是独立的。我尝试了与您的第二个想法类似的方法(参见上面的代码),但没有成功。
  • 这是我的图像文件的存储位置:icap.me/i/ItljCp3Z6c.png 这是我尝试访问它的方式:myPicture = ImageIO.read(getClass().getResource("images/audiorpglogo.png")); 这是我在导出(作为 JAR 文件)并使用 @ 运行时得到的堆栈跟踪987654340@:java.lang.IllegalArgumentException: input == null!
  • 您想要一份代码副本,以便尝试使其工作吗?
  • @PogoStick29 从 IDE 或控制台运行时是否出现此错误?
  • 当我使用java -jar从控制台运行它时出现错误
猜你喜欢
  • 2016-03-14
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
相关资源
最近更新 更多