When I want to load images, locally, I use use:

ImageIcon qmarkIcon = new ImageIcon("images/mark.gif");

When I run the program through eclipse, everything works fine--all the images load like normal, but when I package the executable .jar and run it in a folder without images/*.gif's, none of the images load..... If it is in the folder with images/*.gif's it works again...

So my problem is, how do I get the images to load from ("myjar.jar/images/*.gif) instead of (root/images/*.gif) so they are all packaged on the go, say if someone wants to download my application on the internet?

I made sure to package "image/*.gif" into my jar file, but it just doesn't use it... It still looks in the root directory where the jar file exists.....

Any suggestions?

Recommended Answers

All 19 Replies

If you copy the folder where the images are to a directory the JAR can find it the images will work. HTH

Eg. Java class files and images in C:/my docs blah/blah/etc.... copy the images to a new folder and put the jar either in or outside the folder and it will find the pictures. You may need to alter your code a bit though, post it and I'll check that out..

Ezzaral you are awesome ! you know so much!

I did this: ImageIcon flagIcon = new ImageIcon(getClass().getResource("images/flag2.gif")); and when I go to run my program in eclipse, I get java.lang.NullPointerException... I compiled the executable jar file anyway, and now it doesn't run...

I went back to check that I compiled the jar file correctly with all the image files and I'm sure I did.. It's root contains: images/*.gif META-INF/*.mf *.class

I also verified my MAIN-CLASS and it is is one of the the *.class files in the root...

What's going on here?

EDIT:
I tried exporting the .jar through Eclipse this time instead of using the afs system at my school and it gave me this error:

JAR creation failed. See details for additional information.
Exported with compile warnings: /minesweeper/src/MineSweeper.java
Resource is out of sync with the file system: '/minesweeper/images/flag2.GIF'.
Resource is out of sync with the file system: '/minesweeper/images/mine.GIF'.
Resource is out of sync with the file system: '/minesweeper/images/qmark.GIF'.

Yes, I'm still finishing up that minesweeper game :)

As I don't use Eclipse, I couldn't tell you exactly how to get that folder onto your build path correctly, but the getResource() method attempts to locate a resource based on your classpath. This means that the path you specify must be relative to one of those entries. If your images in the jar file are within a directory called "images", then the code you are using above should locate them just fine.

Try moving your images directory into your source('src') directory and see if that rectifies the build and resource location problem.

to load .gif files from a seperate package I used the below code.

new ImageIcon(ClassLoader.getSystemResource("powerpaint/icons/pencil_bw.gif")));

Try moving your images directory into your source('src') directory and see if that rectifies the build and resource location problem.

That's how I did it actually...

When I use
ImageIcon flagIcon = new ImageIcon("images/flag2.gif");
My program runs without a hitch as long as I have the images/ folder next to the executable jar.. BUT when i move the jar file or the images folder away from each other, my program runs, but without images even though there's a copy of the images folder INSIDE the jar as well, next to the main class file..

From what I read and what you're telling me,
ImageIcon flagIcon = new ImageIcon(getClass().getResource("images/flag2.gif"));
should look inside the jar file instead of outside, but my program gives me java.lang.NullPointerException instead....

I even tried MineSweeper.class.getResource(blah) in case the inner class was a problem.. No luck....

I would try to verify that the images package(directory) is actually in the jar file. Depending upon how you created the jar, those files may have gone into the root of the jar, in which case "images/flag2.gif" would not resolve correctly.

jar cmf mainClass.txt MineSweeper.jar *.class images/*.gif

puts the folder there and everything... Extracted it too, it's there...

Not sure what to tell you then, because that exact jar structure and loading code works just fine for me.

Here, maybe you can take a look at my code... I cut out most of the gooie/extra stuff so you can text it out..

Right clicking should show a flag then a ? then nothing.

If you extract the jar file, you will find my source code, the images, and the compiled .class files. If you run it, the program runs, but the icons dont show.
http://www.fileden.com/files/2008/2/4/1739090/MineSweeper.jar

I'm not using getResource atm so extracting the jar and moveing the image folder next to the jar will show icons. When I try to use getClass().getResource(), i get nullpointererror...

Hopefully this helps you see what I see..

Changing that code to use getClass().getResource() works perfectly on my machine. Attaching the revised jar (I had to put it in a zip file since it won't allow me to attach a .jar)

commented: tank you! +1

copying the code right out of the java file into eclipse gave me the same problem so I decided to delete the entire project and start copying the code from the java files from scratch. Now it's working.....

meh.. Thanks for all your time and help Ezzaral. you deserve more rep points :)

I know this post has been a while back, but maybe somebody else will stumble upon this having the same problem, well I did.
I also had trouble loading the image which was in included in the jar file in the folder images. I had to slighlty change the path to the image for it to work.

An extra slash is needed if the images folders lies in the root directory, so

not:
"images/myImage.png"
this is correct:
"/images/myImage.png"

hope this saves someone else the time I had to waste on this problem :)

I understand that this topic is a bit old, but I figure it's worth responding with the solution.

The issue was in Eclipse, it wasn't recognizing that you had pasted your images folder into your src folder. In order to fix this, all you have to do is right click on the project (in the Package Explorer in Eclipse) and hit Refresh.

Yea, that's the correct way to do it.

I had my images in the src folder, but eclipse just wasn't working right. When I deleted the project and imported the files from scratch, it finally went through. Don't know what happened, but recreating the project fixed it.

BTW, I've attached the latest version of my MineSweeper game. All the source code can be found inside the zip, along with the playable jar which contains the images.

This was my first ever GUI and I couldn't have done it without this board and everyone's help :).

The only way that I have found to have an app work correctly both when executed from within Eclipse and when executed from a jar built by Eclipse is to have two copies of the resource - one in the bin directory of the project and one in the src directory of the project. Eclipse uses the bin directory as the root of the classpath for the app. The jar file uses the root directory in the jar. Eclipse copies non-java files from the src directory into the root directory of the jar when it builds the jar.

Here is a snippet from a method that I added to a JFrame-derived class:

public void setIconImage(String imagePath){
		
		//This method sets the icon for a JFrame. It works both in Eclipse and
		//in a jar file created from the app IF the image file resides in BOTH
		//the bin AND the src directories of the project. Eclipse uses the one
		//from the bin directory (set as the class path when you execute the program).
		//Eclipse copies non-java files from the src directory into the root of the jar when you 
		//export the project. The root of the jar is the base of the classpath when
		//you execute the jar, so the copied image is found at that time.
		
		//The Path name must be specified with a leading "/". That will cause
		//the class loader to look for the resource starting with the root of the
		//classpath.
		//Example in this app: 
		//		inst.setIconImage("/FrameIcon.gif");
		//
		//The icon must be a GIF, JPEG or PNG file (not a .ico file).
		
		try
		{
			URL url = Class.class.getResource(imagePath);
			if (url == null){
				JOptionPane.showMessageDialog(null,"Unable to locate/access '" + imagePath +"' when setting application icon.");
			}
			else{
				javax.swing.ImageIcon imgIcon = new javax.swing.ImageIcon(	url);
				setIconImage(imgIcon.getImage());
			}
		} catch (Exception eError)
		{
			JOptionPane.showMessageDialog(null,"Error occurred when processing: " +
					imagePath + ".\n\nException message: " + eError.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
		}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.