Search code examples
javaimageswingembedded-resourceexecutable-jar

Executable jar doesn't show up the icons/images used in the application


While writing my application (used Eclipse as my IDE), I didn't realize the problem that my images would not be eventually bundled in the jar. Now after completing the project and creating the executable jar, I bumped into this problem. I have around 50 java classes, and I have used images/icons extensively in most of these classes (e.g. in menus, as JLabel icon, in buttons, as tab icons, etc). I have used the ImageIcon API in most of the cases. One example:

JLabel myHeaderImage = new JLabel(new ImageIcon("images/myHeader.jpg"));

What's the best way to pull in the images in the jar. Do I need to change the way I have used the images in my project?


Solution

  • Use getResource():

    JLabel myHeaderImage = new JLabel(new ImageIcon(getClass().getResource("/images/myHeader.jpg")));