How to include all images in jar file using eclipse

I created a java application and linked all the classes in a jar file. When I run the project from eclipse, my application runs successfully. But when I try to run my .jar file, I do not get the icons used by my application. In the code, I get my icons from the catalog of images present in the project folder. How can I present these image files to the end user when using a banner?

I load the image like this:

  final public ImageIcon iReport=new ImageIcon("images/Report.png"); 

I also tried

 final public ImageIcon iquit=new ImageIcon(getClass().getResource("images/quit.png")); 

and

 final public ImageIcon iquit=new ImageIcon(getClass().getResource("/images/quit.png")); 

But this leads to an error:

 Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source) 
+8
java eclipse jar
source share
4 answers

You need to get it from the class, not from the local file system on disk.

Assuming images is actually a package and this package is inside the same JAR as the current class, then do this:

 final public ImageIcon iReport = new ImageIcon(getClass().getResource("/images/Report.png")); 
+11
source share

Files in jar files are processed as "Resources". you need to access them as a class path resource, the usual file access methods do not work there.

Try the following:

 final public ImageIcon iReport = (new ImageIcon(getClass().getResource("images/Report.png"))); 
+2
source share

I know this was asked a long time ago, but it can help others with the same problem as me. I have already used getClass (). GetResource ("..."), but the resource was not exported with the .jar file. I solved the problem by updating the Resources folder and each of my subfolders.

+1
source share

100% works

 final public ImageIcon iReport = new ImageIcon(getClass().getResource("/Report.png")); 

Do not forget about the "/" in path for the image.

+1
source share

All Articles