• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

applet in jar does not work

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic