1

I'm trying to export my java file as a runnable .jar file using Eclipse. The procedure I'm doing is:

File > Export > Java > Runnable .jar, and my options are as follows: http://gyazo.com/88ad22e9382e964e47a1a21200ff0f4b

My problem is, when I run the file in Eclipse, any imported pictures (e.g. a logo for the GUI application) shows up, but it doesn't show up in the exported .jar file.

I.e:

http://gyazo.com/4092e8756f4054ce00429d50c33c22af - in Eclipse http://gyazo.com/7309d84f64832b22560ef023c9349a52 - running the .jar file

My images are in a folder called filesrc which is in the same folder as my project, as can be shown here:

http://gyazo.com/20f8191ee173e06638811c237f985c6f

What am I doing wrong? How can I make these images appear in the .jar?

Thanks

  • 1
    A Jar file is a zip file, you can unzip to check to if the images have been included. My guess is, you're not loading the images using `Class#getResource` but are treating them as plain files (and using `src` in the path reference) – MadProgrammer Jun 23 '15 at 02:02
  • `code imgLabel = new JLabel(new ImageIcon("filesrc//mainlogo.png"));activityLevelHelp = new JLabel(new ImageIcon("filesrc//question-mark.png")); ` That's how I'm referring to them. I don't think I did load the images using Class#getResource. How would I go about doing that? When I unzip the files, the mainlogo.png is there, but I also have alot of other useless files such as different .java files in the same project. Is this normal? What do I do if I only want to export a certain package and the filesrc folder? –  Jun 23 '15 at 02:05
  • Is `filesrc` copied into the jar file? If not, do you copy `filesrc` with the .jar file? Where in the .jar file is `mainlogo.png` located? Is it within the `filesrc` directory? `ImageIcon(String)` expects the `String` to represent a file on the disk, when the files are included in the .jar file, they are no longer "files" – MadProgrammer Jun 23 '15 at 02:06
  • No, I don't believe filesrc is copied into the jar file as it is not there when I unzip (is this how I check). As far as I know, no to the second question also. This is the first time I've ever created a runnable .jar so there is a high chance I've done something wrong. –  Jun 23 '15 at 02:07
  • Try using `ImageIcon(getClass().getResource("/filesrc/mainlogo.png"));` or `ImageIcon(getClass().getResource("/mainlogo.png"));`. Personally, I prefer `ImageIO.read` as this will throw an `IOException` when the image can't be read for some reason – MadProgrammer Jun 23 '15 at 02:08
  • I added `imgLabel = new JLabel(new ImageIcon(getClass().getResource("/mainlogo.png")));` but this throws an error when trying to run in Eclipse: Also tried the first way FWIW. `Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(Unknown Source) at v2.BmrCalcv2.(BmrCalcv2.java:133) at v2.BmrCalcv2.main(BmrCalcv2.java:450)` –  Jun 23 '15 at 02:11
  • Should my filesrc folder be in the src folder? –  Jun 23 '15 at 02:15
  • Personally, that's where I'd put it – MadProgrammer Jun 23 '15 at 02:18
  • So a seperate folder within the src folder, or should I just have all my images in the same folder as the src folder? –  Jun 23 '15 at 02:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81224/discussion-between-madprogrammer-and-ftahir192). – MadProgrammer Jun 23 '15 at 02:23
  • Wondering if we could carry on from our previous discussion @MadProgrammer. I changed the filesrc folder again, so that the folder is NOT in src, but instead it is in a seperate folder outside of src (called images). http://gyazo.com/b4d717067d4ce99ab3ea6ac91174dfbc < Here is how it looks. My concern is, when entering the filepath, `imgLabel = new JLabel(new ImageIcon(getClass().getResource("/mainlogo.png")));` works, both in eclipse and exporting it as a .jar. Why do I not need to include /images in the path? Any idea? –  Jun 23 '15 at 04:18
  • 1
    It will come down to how Eclipse bundles the content together, from the looks of it, it is taking everything under the project directory and including the contents of those directories, but not the directories themselves – MadProgrammer Jun 23 '15 at 04:30
  • That makes sense, thanks :) –  Jun 23 '15 at 04:38
  • Ran into a really weird problem. If I delete the contents of my bin folder, and do the line `imgLabel = new JLabel(new ImageIcon(getClass().getResource("/mainlogo.png")));`, it won't compile. It throws the error `Error: Could not find or load main class BmrCalcv2` both in Eclipse and via terminal. I have to omit the getClass().getResource and manually build for it to work after this. Any ideas why this might be happening? @MadProgrammer –  Jun 23 '15 at 06:18
  • It throws the error if the image folder is not in the bin folder of the project. –  Jun 23 '15 at 06:19

1 Answers1

0

Make sure your exported .jar file contains the folder with your images. So lets assume that your images are in a folder called \images at the root of your JAR file. In your code, you need to change the way your are getting these images to : Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/plus.png"));

ring bearer
  • 20,383
  • 7
  • 59
  • 72
  • Personally, we should all just stop using `Toolkit.getImage` and `ImageIcon`, as they off load the physical loading of the image into the background and don't provide any feedback to when the image loading fails (without needing a whole bunch more code) and only supports a limited subset of file formats. `ImageIO` is, personally, a better API, just saying – MadProgrammer Jun 23 '15 at 02:19
  • Sorry for this stupid question - is the root of my JAR file just the src folder? –  Jun 23 '15 at 02:21
  • @Ftahir192 Not necessarily, it will depend on how the Jar is generated and might include other resources – MadProgrammer Jun 23 '15 at 02:24
  • Hey @Ftahir192 I just found that this is a duplicate question. Check this : http://stackoverflow.com/questions/25635636/eclipse-exported-runnable-jar-not-showing-images – ring bearer Jun 23 '15 at 02:27
  • Tried everything there and nothing seems to work. Sigh... –  Jun 23 '15 at 02:51