Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Images not showing up in JAR file!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SJ




PostPosted: Fri Mar 20, 2009 5:59 pm   Post subject: Images not showing up in JAR file!

I just made a small program to test loading images, it works fine in Eclipse, but when I export it to a jar, the images no longer show up! (the jar file runs, but the images just dont show)

I did some research too, it seems like I need to use this...

ClassLoader cl = this.getClass().getClassLoader();
ImageIcon icon = new ImageIcon(cl.getResource("imgs/someImg.png"));

instead of my original code,

ImageIcon icon = new ImageIcon("imgs/someImg.png");

but the first one doesnt even work - it throws a null pointer excpetion because it doesnt even find the image!

My source code are all in src folder, all images are in the imgs folder. I'm wondering maybe it's something to do with the path of these things?
Help please!
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Mar 20, 2009 7:12 pm   Post subject: RE:Images not showing up in JAR file!

are src and imgs in the same folder, or is imgs in source? Because right now, imgs would have to be in src for it to work. Eclipse stores it as a project, and automatically goes to that folder. I think it's "../imgs/someImg.png". Maybe not. There is a command to go up one file though.
btiffin




PostPosted: Fri Mar 20, 2009 7:52 pm   Post subject: RE:Images not showing up in JAR file!

Have you read this page? It's got a lot of deets.

http://java.sun.com/j2se/1.4.2/docs/guide/resources/resources.html

Cheers
SJ




PostPosted: Fri Mar 20, 2009 11:11 pm   Post subject: RE:Images not showing up in JAR file!

Thank you so much insectoid! I put the imgs folder in the src folder and it worked Very Happy

and thanks btiffin, interesting page, i'll be sure to study it later
Insectoid




PostPosted: Sat Mar 21, 2009 11:56 am   Post subject: RE:Images not showing up in JAR file!

It's good practice to only put source in the src folder, binaries in the bin folder and images in the imgs folder, etc. the ../ is the way to go (if it works in java....)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: