• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Exporting to executable jar, not showing icons

 
Ranch Hand
Posts: 42
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am using Eclipse as my IDE and everything works just as expected unfortunately when I export the project to a executable JAR is fails to display the icons, can some one advise what I am doing wrong.

This is a couple of the places I expect to see ICONS.

static JMenuItem mnuItemQuit = new JMenuItem("Quit", new ImageIcon("res/images/quit.png"));
static JMenuItem mnuItemAbout = new JMenuItem("About", new ImageIcon("res/images/about.png"));


res has been added as a library resources and everything looks good until I export to JAR.

Many Thanks.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the structure of the JAR file you have created? Can you show us what folders/files are in there please?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ImageIcon constructor you are using takes a String parameter for the filename. Resources insinde a jar aren't files. You need to use the ImageIcon(URL) constructor.

Have you discovered the Oracle tutorials? There's a section that explains all this: Loading Images Using getResource
 
Ricardo Wagemaker
Ranch Hand
Posts: 42
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:What is the structure of the JAR file you have created? Can you show us what folders/files are in there please?




Project/src/package/files.java

Added as a library--

Project/res/audio/....
Project/res/images/....

As I say it works perfect in Eclipse until I export it to runnable JAR

P.S.

I tried:

/res/images/...
re/images/...
/images/...
images/...
just_file_name.png
 
Ricardo Wagemaker
Ranch Hand
Posts: 42
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem solved.

ImageIcon iconQuit =new ImageIcon(getClass().getResource("/images/quit.png") );

public JMenuItem mnuItemQuit = new JMenuItem("Quit", iconQuit );


Changed the JMenuItem to public rather than static and then used the getResources to load the images.

Exported to JAR and it all works.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic