User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 374,004 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,759 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1108 | Replies: 15
Reply
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

Loading images from executable jar file?

  #1  
May 7th, 2008
When I want to load images, locally, I use use:
  1. ImageIcon qmarkIcon = new ImageIcon("images/mark.gif");

When I run the program through eclipse, everything works fine--all the images load like normal, but when I package the executable .jar and run it in a folder without images/*.gif's, none of the images load..... If it is in the folder with images/*.gif's it works again...

So my problem is, how do I get the images to load from ("myjar.jar/images/*.gif) instead of (root/images/*.gif) so they are all packaged on the go, say if someone wants to download my application on the internet?

I made sure to package "image/*.gif" into my jar file, but it just doesn't use it... It still looks in the root directory where the jar file exists.....

Any suggestions?
Last edited by TheWhite : May 7th, 2008 at 3:54 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Bristol, UK
Posts: 1,122
Reputation: majestic0110 is on a distinguished road 
Rep Power: 3
Solved Threads: 46
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Veteran Poster

Re: Loading images from executable jar file?

  #2  
May 7th, 2008
If you copy the folder where the images are to a directory the JAR can find it the images will work. HTH
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote  
Join Date: Oct 2007
Location: Bristol, UK
Posts: 1,122
Reputation: majestic0110 is on a distinguished road 
Rep Power: 3
Solved Threads: 46
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Veteran Poster

Re: Loading images from executable jar file?

  #3  
May 7th, 2008
Eg. Java class files and images in C:/my docs blah/blah/etc.... copy the images to a new folder and put the jar either in or outside the folder and it will find the pictures. You may need to alter your code a bit though, post it and I'll check that out..
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,494
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 9
Solved Threads: 251
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Nearly a Posting Maven

Re: Loading images from executable jar file?

  #4  
May 7th, 2008
Use getResource() to load the images instead of the simple path
  1. ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
More info on this here if you want to understand the nuances: http://java.sun.com/docs/books/tutor...ml#getresource
Last edited by Ezzaral : May 7th, 2008 at 1:15 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Bristol, UK
Posts: 1,122
Reputation: majestic0110 is on a distinguished road 
Rep Power: 3
Solved Threads: 46
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Veteran Poster

Re: Loading images from executable jar file?

  #5  
May 7th, 2008
Ezzaral you are awesome ! you know so much!
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

Re: Loading images from executable jar file?

  #6  
May 7th, 2008
I did this:
ImageIcon flagIcon = new ImageIcon(getClass().getResource("images/flag2.gif"));

and when I go to run my program in eclipse, I get java.lang.NullPointerException... I compiled the executable jar file anyway, and now it doesn't run...

I went back to check that I compiled the jar file correctly with all the image files and I'm sure I did.. It's root contains: images/*.gif META-INF/*.mf *.class

I also verified my MAIN-CLASS and it is is one of the the *.class files in the root...

What's going on here?



EDIT:
I tried exporting the .jar through Eclipse this time instead of using the afs system at my school and it gave me this error:

JAR creation failed. See details for additional information.
Exported with compile warnings: /minesweeper/src/MineSweeper.java
Resource is out of sync with the file system: '/minesweeper/images/flag2.GIF'.
Resource is out of sync with the file system: '/minesweeper/images/mine.GIF'.
Resource is out of sync with the file system: '/minesweeper/images/qmark.GIF'.

Yes, I'm still finishing up that minesweeper game
Last edited by TheWhite : May 7th, 2008 at 2:11 pm. Reason: new information
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,494
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 9
Solved Threads: 251
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Nearly a Posting Maven

Re: Loading images from executable jar file?

  #7  
May 7th, 2008
As I don't use Eclipse, I couldn't tell you exactly how to get that folder onto your build path correctly, but the getResource() method attempts to locate a resource based on your classpath. This means that the path you specify must be relative to one of those entries. If your images in the jar file are within a directory called "images", then the code you are using above should locate them just fine.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,494
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 9
Solved Threads: 251
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Nearly a Posting Maven

Re: Loading images from executable jar file?

  #8  
May 7th, 2008
Try moving your images directory into your source('src') directory and see if that rectifies the build and resource location problem.
Last edited by Ezzaral : May 7th, 2008 at 3:36 pm.
Reply With Quote  
Join Date: Jan 2008
Posts: 15
Reputation: mrynit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mrynit's Avatar
mrynit mrynit is offline Offline
Newbie Poster

Re: Loading images from executable jar file?

  #9  
May 7th, 2008
to load .gif files from a seperate package I used the below code.

new ImageIcon(ClassLoader.getSystemResource("powerpaint/icons/pencil_bw.gif")));
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

Re: Loading images from executable jar file?

  #10  
May 8th, 2008
Originally Posted by Ezzaral View Post
Try moving your images directory into your source('src') directory and see if that rectifies the build and resource location problem.


That's how I did it actually...

When I use
ImageIcon flagIcon = new ImageIcon("images/flag2.gif");
My program runs without a hitch as long as I have the images/ folder next to the executable jar.. BUT when i move the jar file or the images folder away from each other, my program runs, but without images even though there's a copy of the images folder INSIDE the jar as well, next to the main class file..

From what I read and what you're telling me,
ImageIcon flagIcon = new ImageIcon(getClass().getResource("images/flag2.gif"));
should look inside the jar file instead of outside, but my program gives me java.lang.NullPointerException instead....

I even tried MineSweeper.class.getResource(blah) in case the inner class was a problem.. No luck....
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 10:21 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC