1

I am a novice programmer trying to create a jar file that people can download and demonstrates a...JRPG-ish upgrade system I have. As a little visual pop, I want the central button to display a little icon as an extra visual pop. While it works fine in Eclipse, when I export it to a JAR file the icon is no longer displaying (though the rest of the program works as intended.

Eclipse vs JAR

I have managed to make Eclipse import the files I want into the JAR file, as seen below.

Overview of project

This is the code I currently have to display them in Eclipse

imgDin = new ImageIcon("icons\Mark_of_Din.png");
imgNayru = new ImageIcon("icons\Mark_of_Nayru.png");
imgFarore = new ImageIcon("icons\Mark_of_Farore.png");

How can I modify this code to display them inside the JAR file?

chif-ii
  • 995
  • 1
  • 6
  • 12
  • 1
    Please check this: http://stackoverflow.com/questions/31127/java-swing-displaying-images-from-within-a-jar – home Nov 09 '14 at 17:09

1 Answers1

3

You should be using

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

That little package icon in the icons folder icon, means the files in the icons folder will be at the root of the classpath. getClass() gets the calling class, and getResource() locates a resource relative to the class. The / brings the search to the root.

You can extract the jar and you will see the icons folder is not there.


"While it works fine in Eclipse.."

That's because when you pass a String to the ImageIcon constructor, it is looking for a file on the file system, relative to the working directory. In Eclipse (and most IDEs), the default working directory is the project root, and that's where your icons folder is.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • your answer is reasonable that is why I have deleted my answer, thanks dear for correction, I am voting you up :) – Muhammad Nov 09 '14 at 17:31