2

I have a problem: I put my images in my project folder, for example, E:\All Work IT\Java All\JavaWork\TestingDB and when I use this line of code to use the images

ImageIcon icon = new ImageIcon("start.png");

It works perfectly! But when I want to make a Runnable jar file (Export -> select Runnabable jar file) and open the specific jar, the images don't appear on the buttons, menu, and so on.

Where do I need to put my images to solve this problem, and with what code? Thanks.

hsirkar
  • 769
  • 1
  • 8
  • 19
  • Put the images within the context of jar (so they embedded) then use Class#getResource to load them. How you include the images in your jar will depend on how you are building the jar – MadProgrammer Mar 10 '15 at 21:48
  • 1
    Please can you confirm which, if any IDE your using. – Adam Mar 11 '15 at 05:16

2 Answers2

2

new ImageIcon(file) specifies a local file which won't work inside a JAR.. Try changing the line to use a the class loader.

 new ImageIcon(getClass().getResource("/start.png"));

If you're in a static context

 new ImageIcon(Foo.class.getResource("/start.png"));

I'm unsure of your project structure or IDE, if any, so I'm unable to make any recommendations on where images should go. Just make sure the images are included in the JAR export and end up at the top level, or structure matching your getResource() call

Adam
  • 35,919
  • 9
  • 100
  • 137
  • Read the question - the images don't exist within the jar – MadProgrammer Mar 10 '15 at 21:49
  • I assumed E:\All Work IT\Java All\JavaWork\TestingDB was the IDE project, and that the generate runnable JAR would have picked them up... especially as the code new ImageIcon("start.png") managed to find them, implying they were in the current directory... – Adam Mar 10 '15 at 21:51
  • 1
    The images are found, because they are with in the relative execution context of the program, that doesn't mean they located with the context if the project. So, if you're very lucky, you've answered half the question. Assume makes a "ass" out of "u" and "me" - no offense intended, it's something one of mentors says to me all time. Don't assume anything, either cover all the possibilities (as much as possible) or ask questions for clarification – MadProgrammer Mar 10 '15 at 22:01
  • Surely it seems unlikely that a novice developer has reconfigured their launch config (assuming eclipse) to change the current working directory to some other location on disk. It would default to the project directory... Also question states " I put my images in my project folder". By default Create Runnable JAR in eclipse mops up everything in project directory... – Adam Mar 10 '15 at 22:08
  • Again, you're assuming a context where no evidence exists. What if they are using NotePad++ and are hand building the project? There's nothing to suggest anything about how the OP is building there project...*"By default Create Runnable JAR in eclipse mops up everything in project directory"* - From my experience with all the other questions related to Eclipse and missing resources (and not using Eclipse myself) that doesn't seem to be correct - but that's based on the questions people ask about Eclipse and missing resource and not experience with Eclipse itself. – MadProgrammer Mar 10 '15 at 23:08
  • Don't forget, despite what might be going on your world, not everybody uses Eclipse or Maven and some people (as crazy as it might sound) don't use IDE's at all. – MadProgrammer Mar 10 '15 at 23:16
  • Question used terminology "create runnable jar" which has since been updated to "file... export... create runnable jar". Which kind of implies ide unless notepad has that now. My axperience of eclipse feature of same name is that top level files do get swept up. – Adam Mar 11 '15 at 05:22
  • Well, you're two example structures won't work for my working environment. You're still assuming context... – MadProgrammer Mar 11 '15 at 05:25
  • I've removed examples in case they were misleading – Adam Mar 11 '15 at 05:34
1

One if the simplest ways would be to include the images within the jar file itself, this reduces the number of resources you'll need to deploy; much easier to deploy a single jar then a jar file and a bunch if images

It also reduces the possible issues with execution context and having to calculate the relative paths

Who you achieve this will depend on how you are building the jar, for instance in Netbeans and Eclipse, you can copy the images into the src directory. In maven, you'd need to include the images in the resources directory

This will change the way you need to use to load the images or resources

You will need to use Class#getResource or Class#getResourceAsStream depending in your needs, for example

ImageIcon img = getClass().getResource("/start.png");

Remember, you will need to use the full path to the image from the context of the src directory

This means if you place the resources within the images directory under the src directory, then you will need to use /images/start.png for example

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I am sorry for the confusion! Like I said to Adam, I use Eclipse IDE. and that path that I put in my question, it was my project folder created in Eclipse. I try to create a folder named resources in src folder, and in that new folder I put my all images. After that I use this line of code ImageIcon icon = new ImageIcon(getClass().getResource("/resources/start.png"); And I have the same behavior. The jar file I created in this way: Right click on project, select Runnable jar and then click on finish after I select Extract required libraries into generated JAR I am sorry because I don't know – AurelianStefan Mar 11 '15 at 05:59
  • The resulting `jar` file is just a zip file. Try unzipping it and seeing if the images are within the `resources` directory – MadProgrammer Mar 11 '15 at 06:04
  • I extract it, and I don't have the folder images.. why happening that? I mention that I have a package in my project folder named : testing.db.stef and when I extract the zip I have the meta-inf, org and testing folder. – AurelianStefan Mar 11 '15 at 06:12
  • And you put the `resources` file in the `src` folder? – MadProgrammer Mar 11 '15 at 06:13
  • 1
    You might find [this](http://stackoverflow.com/questions/25635636/eclipse-exported-runnable-jar-not-showing-images) of some help – MadProgrammer Mar 11 '15 at 06:14
  • I put my folder-images in src folder and like I said, after I unzip I have just testing folder.. I try to put the images-folder in testing folder, and after I unzip, this folder with images, doesn't exist.... – AurelianStefan Mar 11 '15 at 06:14
  • Thank you so much Mr. MadProgrammer. Your link helped me. I use the first example and it work perfectly even after I make a runnable jar file. Thanks again! – AurelianStefan Mar 11 '15 at 06:38