File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "applet in jar does not work" Watch "applet in jar does not work" New topic
Author

applet in jar does not work

Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
Hi,
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!
-Karen.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[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
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
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.


Ping & DNS - my free Android networking tools app
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.

-Karen.
 
jQuery in Action, 2nd edition
 
subject: applet in jar does not work
 
Similar Threads
Jar file download status
Applet runs only if class file is in the same dir
Specify classpath when run class in jar file
running an applet from a jar
jar file