wood burning stoves 2.0
The moose likes Applets and the fly likes applet in jar does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "applet in jar does not work" Watch "applet in jar does not work" New topic

applet in jar does not work

Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
I have a newbie (to applets, anyway) question about using jar files. I have a very simple applet with one .gif file, which works fine using the applet tag and no jar.

When I jar the applet+gif, and then try to run the applet, I get the "applet notinited" message.

Here is the command I'm using the jar the file:
>jar -cvf myjar.jar ImageApplet.class mouse.gif
(my understanding is that I don't need to modify the manifest file since its an applet?)

And the applet tag:
<applet code="ImageApplet.class" archive="myjar.jar" height=160 width=500>

The html file and the jar file are currently in the same local directory, and I cleared the cache to make sure that's not the problem.

Any help would be greatly appreciated!
Joe Ess

Joined: Oct 29, 2001
Posts: 9150

Have a look at the Java console (if you are using the Java Plugin, there will be a teacup in the system tray). I'll bet your applet can't find the GIF file when it's jarred.

[How To Ask Questions On JavaRanch]
Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
OK, but how do I tell the applet how to find the image in the jar? I'm using getCodeBase() now. Here is my code:

Thanks again, Karen.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
getCodeBase() refers to the web directory where the web page containing the applet is located. If the image is not directly inside that directory, that won't help.

The following paragraph describes what to do (I've copied it from a list message years ago, and have not tried it, but it should point you in the right direction):

>To load an image from a jar file (platform independent) you can use the
>following method or a similar one:
>public static final String myImageFile = new String("/Resources/Misc/imagename.gif");
>public Image getMyImage() {
> return Toolkit.getDefaultToolkit().getImage(getClass().getResource(myImageFile));
>obviously the imagename.gif must be in a folder Resources/Misc in the jar file.
>The essential call is the
>getClass().getResource(String image)
>that will return the correct URL to load the image file from the jar.
>Important: I believe (never tried a different way) that the class file
>calling the method must be in the same jar archive of the images.
Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
Thank you, I will give it a try! For the moment, I've just put the gifs outside of the jar and it works OK.

I agree. Here's the link: http://aspose.com/file-tools
subject: applet in jar does not work
It's not a secret anymore!