DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Loading images from executable jar file? (http://www.daniweb.com/forums/thread122909.html)

TheWhite May 7th, 2008 3:51 am
Loading images from executable jar file?
 
When I want to load images, locally, I use use:
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?

majestic0110 May 7th, 2008 10:39 am
Re: Loading images from executable jar file?
 
If you copy the folder where the images are to a directory the JAR can find it the images will work. HTH

majestic0110 May 7th, 2008 10:40 am
Re: Loading images from executable jar file?
 
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..

Ezzaral May 7th, 2008 1:15 pm
Re: Loading images from executable jar file?
 
Use getResource() to load the images instead of the simple path
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

majestic0110 May 7th, 2008 1:34 pm
Re: Loading images from executable jar file?
 
Ezzaral you are awesome ! you know so much!

TheWhite May 7th, 2008 1:46 pm
Re: Loading images from executable jar file?
 
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 :)

Ezzaral May 7th, 2008 2:56 pm
Re: Loading images from executable jar file?
 
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.

Ezzaral May 7th, 2008 3:36 pm
Re: Loading images from executable jar file?
 
Try moving your images directory into your source('src') directory and see if that rectifies the build and resource location problem.

mrynit May 7th, 2008 8:35 pm
Re: Loading images from executable jar file?
 
to load .gif files from a seperate package I used the below code.

new ImageIcon(ClassLoader.getSystemResource("powerpaint/icons/pencil_bw.gif")));

TheWhite May 8th, 2008 3:55 pm
Re: Loading images from executable jar file?
 
Quote:

Originally Posted by Ezzaral (Post 602041)
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....


All times are GMT -4. The time now is 11:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC