tags:

views:

188

answers:

4

I need it to run without having the files exported to the computer.
At the moment, my code for storing the images is:

ImageIcon icon = new ImageIcon("images\\images2.gif");

It can't just be an image since I'm adding it to a JLabel.
When I jar the entire program, it stores the image files in the jar.
When I go to run the actual problem, there are no images.

Again, I can't just leave the .jar in a folder with the images already. It has to work on a separate computer, by itself.

+3  A: 

You need to access those files through class-loader... Something like this:

InputStream is = this.getClass().getClassloader().getResourceAsStream("images/image.ico");

HTH

UPD: note, that this will work both with JARed package and with plain directory structure.

archimed7592
Where would i go from this? eventually i add the ImageIcon to a JLabel.
tally
You don't the "this" in front of method invocations.
Steve Kuo
You should change it to getResource() instead of getResourceAsStream(), since ImageIcon takes an URL, not an InputStream (as suggested by Steve).
Jorn
A: 

Try this:

ImageIcon icon = new ImageIcon(this.getClass().getClassloader().getResource("images/images2.gif"));

if that doesn't work, replace this.getClass().getClassloader() with MyClass.class where MyClass is the name of your class.

I remember having to edit this slightly to make it work in Eclipse, but when you deploy it, it works like a charm.

Edit: To make it work in Eclipse, you may need to change it to:

ImageIcon icon = new ImageIcon(this.getClass().getClassloader().getResource("bin/images/images2.gif"));

If that doesn't work, do the standard, replace this.getClass().getClassloader() with MyClass.class. If it still doesn't work, try replacing "bin" with "src". Try jar'ing it with the first way and see what happens.

ygd
Is that supposed to be a / rather than a \?
Laurence Gonsalves
I can't get access to a getClassLoader() function. I do have java.lang.* imported, but it doesnt help. The MyClass.class didn't work either.
tally
If you don't have a Class.getClassLoader() method, then you are not using a standards-conforming Java. It is never necessary to import java.lang either. I suspect that your code runs in a static context and what does not work is using "this" - as the answer says, use a class literal then.
Michael Borgwardt
+9  A: 

You'll want to get the image via the system class loader:

URL url = ClassLoader.getSystemClassLoader().getResource("images/images2.gif");
Icon icon = new ImageIcon(url)

images is at the root of the classpath.

Note that the Java runtime will translate the separator (/) to the OS specific separator (\ for Windows).

Steve Kuo
This is the correct answer, but for some reason the system won't let me vote up, saying my vote is too old. But I currently don't have a vote for this answer...
Jorn
A: 

The basic issue is that the File class only knows how to work with what the underlying operating system consider a file, and a whole one.

A jar file is essentially a zip file with some extra information so you cannot use File's with that. Instead Java provides the "resource" concept which roughly translates to "a chunk of bytes or characters which we don't care where is, as long as we have them when we need them". You can ask the class loader for any resource in the class path - which is what you want here - or access it through an URL.

Thorbjørn Ravn Andersen