Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

applet in jar does not work

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9612
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karen Nelson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic