图像未加载到可执行jar中 [英] Images not loading in executable jar

查看:116
本文介绍了图像未加载到可执行jar中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我确实尝试了 ImageIcon background = new ImageIcon(getClass()。getResource(MyImage.jpg));
在所有答案中都提到了。

Note: I did try out ImageIcon background = new ImageIcon(getClass().getResource("MyImage.jpg")); mentioned in all the answers.

我的项目使用netbeans IDE运行正常。但是一旦我使用'Clean and build'导出到可执行jar,就会抛出空指针异常,并且带有图像的帧甚至都不会打开。
(可执行jar在'dist'文件夹中)

My project works fine using netbeans IDE run. But once I export to an executable jar using 'Clean and build', A null pointer exception is thrown and the frame with the image will not even open. (The executabe jar is in the 'dist' folder)

即使使用相对路径,图像也永远不会加载。

Even after using relative path, the image will never load.


  1. 我将图像放在与可执行jar相同的目录中。(dist)

  2. 我将图像放在lib文件夹中。 (dist\lib)

  3. 我创建了一个src和res文件夹并将图像放在这些文件夹中。(dist\src& dist\res)

以上都不起作用。

我用winRar打开jar并看到图像存在。但它永远不会加载运行。
任何人都可以帮助我。??

I opened the jar with winRar and saw the image is present. but it never loads on running. Can anyone please help me.??

推荐答案

最后我想出来了。

ImageIcon background = new ImageIcon(getClass().getClassLoader().getResource("MyImage.jpg"));

类加载器如何扮演重要角色!!

Some how class loader plays a huge role !!

同样重要的是准确获取文件名。 .JPG
在IDE中运行,但在导出到JAR时,
必须是名称 .jpg 本身如果我们试图访问 MyImage.jpg

Also it is important to get file name exactly. .JPG is run in the IDE but on exporting to a JAR, It must be name .jpg itself if we are trying to access MyImage.jpg

这篇关于图像未加载到可执行jar中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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