[java] JAR problem

Started by
5 comments, last by Si Hao 17 years, 6 months ago
Recently I finally made a simple interactive game that collects input and move some objects accordingly. It works in Eclipse IDE, but when I JAR it (using Eclipse export function) and runs it a little problem appears. The game does not display any of the images, all I can see is a flashing screen. I cannot figure out what is wrong, the images are inside the jar file in the correct directory, no exceptions, its outputting correct messages. Everything works except the images are not there... Can someone kindly point out what maybe the problem. Thanks in advance
Advertisement
How are you loading your images (post code if possible)? The usual way is to load them from the classpath (eg. getClass().getClassloader().getResourceAsStream() ). Then that'll work both from jar and from disk (for disk you'll probably want to set the classpath to include the root of your data directory.

Other things to watch for are case-sensitive paths (jars are case sensitive, windows isn't), and forward vs. backslash in paths (forward slashes should work everywhere). Also be careful not to have leading slashes for resource paths (eg. "Data/image.png" rather than "/Data/image.png".

Other than that I can only suggest stepping though your loading code and see if anything unexpected crops up.
Sorry for the late reply was quite busy here is my code

bgImage = loadImage("images&#47;background.jpg");Image player1 = loadImage("images&#<span class="java-number">47</span>;player1.png"</span>);<br>Image player2 = loadImage(<span class="java-literal">"images&#<span class="java-number">47</span>;player2.png"</span>);<br>Image player3 = loadImage(<span class="java-literal">"images&#<span class="java-number">47</span>;player3.png"</span>);<br><br><span class="java-visibilitymodifier">private</span> Image loadImage(String filename){<br>	<span class="java-keyword">return</span> <span class="java-keyword">new</span> ImageIcon(filename).getImage();<br>}<br><br></pre></div><!--ENDSCRIPT--> 
And...? What does getImage return? Have you tried to debug this yourself yet?

Personally I'd switch to using ImageIO.read(URL). Then if it fails you should get a slightly more descriptive exception being thrown rather than just failing silently.
getImage() will return a simple ImageIcon to be drawn.

I just tried out ImageIO.read() as you suggested and it works, it seems there might be some issues with ImageIcon. I will research more on it to see if its my implementation (I copied an example from a book or some inherent reason that it doesn't work with JAR.

Thanks for the help and tip [smile]
Please check this out, might be helpful.
a.k.a javabeats at yahoo.ca
Thanks alot thats very helpful [smile]

This topic is closed to new replies.

Advertisement