运行导出程序时出现异常,但不在Eclipse中 [英] Exception When Running Exported Program, but not in Eclipse

查看:750
本文介绍了运行导出程序时出现异常,但不在Eclipse中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,并尝试将其导出并发送给某人。当我在Eclipse中运行程序时,它的工作效果非常好。但是,当我将它导出为Runnable JAR文件并双击它时,它将退出并通知我检查控制台。



这是堆栈跟踪:

  javax.imageio.IIOException:无法读取输入文件! 
在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)
在java.lang.reflect.Method.invoke(Method.java:597)
在org.eclipse.jdt.internal .jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
javax.imageio.IIOException:无法读取输入文件!
在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)
在java。 lang.reflect.Method.invoke(Method.java:597)
在org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)






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

  BufferedImage myPicture = null; 
尝试{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! 
在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)


解决方案

您可以尝试这样:

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

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






或者如果要使用放在内的图像 JAR文件中您可以使用

  ImageIcon icon = new ImageIcon(getClass ).getResource( 图像/ 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;

私人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中运行它时,我得到

 线程中的异常mainjava.lang.NullPointerException 
javax.swing.ImageIcon。< init> (MyFrame.java:14)
MyFrame.mava(MyFrame.java:25)
这个Eclipse项目看起来像



图像



code>文件夹由



右键单击项目 - > 导出 - > JAR文件 - > 下一个





next - > next - > select main class





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




I am writing a program and am attempting to export it and send it to someone. When I run the program in Eclipse, it works perfectly well. However, when I export it as a Runnable JAR File and double-click it, it quits and tells me to check the console.

Here's the stack trace:

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)


I think it didn't export the image correctly. Here's how I access the image:

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


Also, when i try

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

I get a stack trace, even when running within 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)

解决方案

You can try this way:

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

this will work if you have images/myimage.jpg directory in the same folder that you are running your application with your JAR file.


or if you want to use image that is placed inside JAR file you can use

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

Don't be afraid when it wont work in IDE. It will work when you run it from JAR file.


Here is some code sample that works for me:

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();
    }
}

When I try to run it in Eclipse I get

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)

this Eclipse project looks like

After i export this project to JAR with images folder by

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

next -> next -> select main class

I double-click created JAR file and see frame with my image:

这篇关于运行导出程序时出现异常,但不在Eclipse中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆