Jar文件执行时出现NullPointerException。什么都行不通 [英] NullPointerException when Jar file executes. Nothing works

查看:205
本文介绍了Jar文件执行时出现NullPointerException。什么都行不通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我已经尝试了一切。这些是我尝试过的代码中的一些示例,它们在IDE中运行得非常好,但是只要它打包在一个jar文件中,就会崩溃。每个文件都在同一级别并打包在jar文件中。我用WinRar打开它,看到图像c3.png绝对在我的Main-Class旁边,在Jar的根目录中。

I feel like I've tried everything. These are some of the examples of the code I've tried which runs perfectly fine inside the IDE, but as soon as it's packaged in a jar file, it falls apart. Every file is on the same level and packaged inside the jar file. I open it with WinRar and see that the image "c3.png" is absolutely inside, right next to my Main-Class, in the root of the Jar.

C3 = new ImageIcon( getClass().getResource("c3.png") );

C3 = new ImageIcon( this.getClass().getResource("c3.png") );

C3 = new ImageIcon( ImageIO.read( getClass().getResource("c3.png") );

C3 = new ImageIcon( ImageIO.read( ClassLoader.getSystemClassLoader().getResourceAsStream( "c3.png" ) ) );

C3 = new ImageIcon( ImageIO.read( getClass().getClassLoader().getResource( "C3.png" ) ) );

编辑:我已经尝试了一些......当然......失败了。

edit: I've tried some more that have... of course... failed.

C3 = new ImageIcon(VP2CPConverter.class.getResource("/C3.png"));

C3 = new ImageIcon( VP2CPConverter.class.getClassLoader().getResource( "C3.png" ) );

在简短的故障排除尝试中,我甚至尝试过getResourceAsStream()在Jar执行时查找可用的每个资源。在IDE中,所有这些资源。在Jar中......没有他们。

In a brief troubleshooting attempt, I even tried getResourceAsStream() to find out every resource available when the Jar executes. Inside the IDE, all of them. In the Jar... NONE of them.

现在有人开枪了。

推荐答案

C3 = new ImageIcon( getClass().getResource("c3.png") );
C3 = new ImageIcon( this.getClass().getResource("c3.png") );

这两个是相同的,并假设资源存在于包名称所代表的文件夹中当前的课程。显然它不是。

These two are identical, and assume that the resource is present in the folder represented by the package name of the current class. Clearly it isn't.

C3 = new ImageIcon( ImageIO.read( getClass().getResource("c3.png") );

这是相同的假设。

C3 = new ImageIcon( ImageIO.read( ClassLoader.getSystemClassLoader().getResourceAsStream( "c3.png" ) ) );

这假设资源存在于JAR文件的根文件夹中。显然它不是。

This makes the assumption that the resource is present in the root folder of the JAR file. Clearly it isn't.

C3 = new ImageIcon( ImageIO.read( getClass().getClassLoader().getResource( "C3.png" ) ) );

这是相同的假设。

C3 = new ImageIcon(VP2CPConverter.class.getResource("/C3.png"));

这是相同的假设。

C3 = new ImageIcon( VP2CPConverter.class.getClassLoader().getResource( "C3.png" ) );

这是相同的假设。

C.所有上述假设都是错误的。因此,将其中一个设为true,并使用相应的代码来假定它。如果要通过当前类获取资源,请将其放入当前类的包中。如果您想通过类加载器或从 / 开始的路径获取它,请将其放入JAR文件的根目录。

Clearly all the above assumptions are false. So make one of them true, and use the corresponding code that assumes it. If you want to get the resource via the current class, put it into the current class's package. If you want to get it via the class loader or a path starting in /, put it into the root directory of the JAR file.

这篇关于Jar文件执行时出现NullPointerException。什么都行不通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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