I'm new to creating jar files. I'm using JCreator and have set up the jar creation and the manifest using winrar and txt to get it to run. However if I run the .jar from anywhere other than the programs root directory all the images vanish. I have been looking at other peoples solutions but none of them are using Images for their graphics and all of mine are loaded with Image.

Image virImage,Background;
//above line is declaration of Image type variables
//the line below is in the constructor loading the image into the variable.
Background = Toolkit.getDefaultToolkit().getImage("images/MapForGame.jpg");

Is it possible or are Image variables unable to be used in jar files? I'd like to avoid converting everything into ImageIcons if I can since I haven't worked with them and it seems like it would be a difficult conversion at this point in the program since I'm using several 100 images.

Thank you in advance for any help.

The posted code looks in a folder relative to the current directory for the image files it is trying to read.
Can you put the images inside of the jar file?
If so, you could treat the image file as a resource and use the ClassLoader's getResourceAsStream() method to get to the image file. See the ImageIO class for a method to use to read the image.


I do have the Images in the folder inside the .jar file. I'm sorry but I don't understand how I can use getResourceAsStream() here. Can you show me a code example on how I would set this up for an image file?

try to create jar file including the image folder

I'm sorry I wasn't clear on my last post but when creating the jar file the image folder was included. But for some reason it isn't looking for the images inside the jar file it's trying to find the image folder in the folder the jar file is in.
Where I want it to look at the folder inside the jar file so I don't have to tell people how to extract the images from the jar file to make them work.

public ImageIcon createImageIcon(String filename, String description) 
        String path = "/resources/images/" + filename;
        return new ImageIcon(getClass().getResource(path)); 

    Try by using resources folder

I do have the Images in the folder inside the .jar file

Use the path to the image file that is inside the jar file in the getResourceAsStream() method and use the InputStream that is returned in the ImageIO class's read() method.

I'm sorry NormR1 but I don't know how to code it to look in the jar file using getResourceAsStream() that's why I asked for a code example.
When i try

Background = getClass().getResourceAsStream("/images/Background");

And I still keep all the other code the same I get an incompatible types error. So how do I use this to get an image?

Softswing is ImageIcon a custom class or is it a built in class in Graphics or Graphics2D. And what is a resources folder or just name the folder resources?
Using the ImageIcon method I'd have to change all 100+ images to ImageIcons right? Then how do you display them can you still use g.drawImage(image var, x, y)?

Thanks for all the help so far I hope I can figure out how to resolve this seems a bit ridiculous that it won't just see the folder that's built into the jar file on its own.

incompatible types error.

Use the ImageIO class's read() method with the InputStream returned by getResourceAsStream().

Do a search for getResourceAsStream here on the forum for code samples.

Thank you NormR1 this is the code that seems to work. I'll have to test it by sending the jar to a friend to test.

//adding import java.net.URL;  to my imports
imageURL = BarbarianPrince.class.getResource("images/MapForGame.jpg");
Background = Toolkit.getDefaultToolkit().getImage(imageURL);

I'd use the ImageIO class's read() method instead of the Toolkit

I couldn't get the read() method to work and Toolkit seems to be working that's why I stuck with it.

If there were error messages, post them here if you want help with them.

What are the benefits of the ImageIO read() method compared to the Toolkit? I'm wondering why use one over the other. If ImageIO read() is much more beneficial then I'll redo the edits and let you know the errors I got. If there really isn't a difference this is working so I'll keep it as is.

There is a question I have about saving files inside a jar file but I haven't got to that part of the program yet. As I'd like to have a high score save when the player dies and save their name with it as well as some info of what thay had at the time. But I'll ask that if I can't figure it out later and if I can't find any examples in the search. Since I don't have a chance to test it till I get to that part of the program.

jar files are not a place to put files you want to update. They hold data/files/classes that are to be read.

Try the toolkit method with an invalid path and then try the ImageIO method to see what the differences are for error conditions.