Right, my program ran perfectly fine with the line

JLabel headerImage = new JLabel(new ImageIcon(img));

But as soon as i put it into a JAR file it has problems, i have searched the inet for soloutions and there isnt much out there for apps, loats for applets though, but none of the solutions seem to work. I currently have got this to work in the app but still wont work when i jar it up.

Image img = null;	
try {
	InputStream in = new InputStream(getClass().getResourceAsStream("./com/images/loginTitle.jpg"));

//	Read from an input stream
             img = ImageIO.read(in);
} catch (Exception e1) {
JLabel headerImage = new JLabel(new ImageIcon(img));

Surly there is a way to access images stored within a jar file, they aren not in a seperate jar in the jar :confused: . They are simply in a package in the main JAR.

Any suggestions?

Recommended Answers

All 5 Replies

I had the same problem a few months ago. The best thing to do is to make a URL like this:

URL imageURL = JARFILENAME.class.getResource("IMAGE.JPG");

Just replace the colored parts to your own stuff...


construct the image like so...

Image myImage = Toolkit.getDefaultToolkit().getImage(imageURL);

Yes, the URL Object should make that work.

Knight, is this JAR file an executable JAR file? If it is, could you show me how you did it? I tried once and never could get mine to work, even though everything was correct.

Im using eclise and exporting it as a jar file. Its fairly easy to do.

The URL thing doesnt work, I am not sure how it is supposed to work anyway, how is it supposed to access a jar file that isnt created until the code is done?, it just gives me these errors :

at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)

Which is understandable as there is no connection etc as this isnt an applet. Please corrct me if im way off the mark but it should work before i JAR it up shouldnt it, it wont compile otherwise. It is all going in one Executable JAR file which is a stndalone application, not a web applet. the images are within that JAR file along with all the other class files. I cant access the JAR file from within the code as I havent created it yet. And i cant create it until it compiles and it wont compile as it is trying to reference things in the JAR which isnt created yet......

I just need to access images from with my jar file...... why do jar files create so much trouble :cry:

with the URL code, it's looking for the images inside of the Jar file, obviousely, if there is no jar file created, then it won't be able to find the images, so it must be created before you try and run the code...
Create a jar file and make sure that the images you want to call are in the jar file. Also, save a text file called 'manifest.txt' into the jar file. 'manifest.txt' should look like this:

Main-Class: (the name of the class that holds your main method)

Make sure that you have a carriage return after that as well.

When you go to compile the jar file, just type in this...:

%jar -cvmf manifest.txt (create name for jar file here) *.class

Thanks a lot brother it worked for me,can't thank you enough for this help.i was using BufferedImage previously and it was not accepting url type but an image does,so it can be accessed in jar.

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.