wood burning stoves 2.0
The moose likes Beginning Java and the fly likes Executable jars! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Executable jars!" Watch "Executable jars!" New topic

Executable jars!

Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128

This is probably a neverending, inexhaustable subject...

I'm trying to jar my game, and all is well until I try to run it. It runs like a charm when doing "java -jar Arkanoid.jar", but it seems my classes can't reach my images, contained in folder Images, and I can't double click on the jar file to start the game.

My game folder looks like:

-all classes
-subfolder Images, contains all images.
-subfolder src, contains all src.

in the arkanoid/ folder, I do the following:

jar cmf MANIFEST.MF Arkanoid.jar *.class Images src

MANIFEST.MF contains one row of text:

Main-Class: GameBoardMultBall[enter]

When I do this, I see that all images are compressed and put into the jar file, but why doesn't it work?

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Without seeing your code, I can only guess that to load an image, you're currently doing something like

java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getImage("images/moosefly.gif");

This won't work when everything is all jarred up. For this situation, you'll want to make use of java.net.URLClassLoader, as described in the "Using JAR-related APIs Introduced in 1.2" lesson of Sun's Java Tutorial.

java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getImage(java.net.URLClassLoader.getSystemResource("images/moosefly.gif"));

I can verify that this does work, as I just tested it.

For more complete steps to creating a working demo, I'll post an addition to the "Creating an Executable JAR File" thread.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Well, darn there, sheriff..!

Since you're a 100% right on how I currently load my images, a good guess would be that your tested solution works just as well! I'll give it a try.

Thanks a bunch!

Sincerely, Kaspar
I agree. Here's the link: http://aspose.com/file-tools
subject: Executable jars!
It's not a secret anymore!