Images in jar files

Posted on 2000-05-16
Medium Priority
Last Modified: 2010-04-01
How can I tell the java interpreter where to look for image files in a Jar file?

For example, I have a class called "MyClass.class" and an image file called "MyImage.jpg".      The constructor of MyClass will create an instance of ImageIcon("MyImage.jpg").     Now I want to package MyClass.class into a Jar file and I also want to include "MyImage.jpg" in that jar file as well.

However, the java interpreter seems to expect MyImage.jpg from the directory where the main Class is stored.    Even though "MyImage.jpg is in the same archive as "MyClass.class", MyClass won't be able to find MyImage.jpg unless there is another MyImage.jpg in the directory where the main Class resides.

How can I tell the java intepreter to look for "MyImage.jpg" in the archive that MyClass.class comes from?    Is there any work-around or something?

Thanks alot.
Question by:mtngu76
  • 3
  • 2

Accepted Solution

jerch earned 150 total points
ID: 2816002
Hi mtngu76...
URL url = MyClass.getClass().getResource("<dir>/MyImage.jpg");

ImageIcon = new ImageIcon(url);

best regards...


Author Comment

ID: 2817942
Hi jerch,

Thanks for the comment.    I actually tried your suggestion and it did work, except that I think MyClass.getClass() should be MyClass.class instead.


Expert Comment

ID: 2819940
Oh sorry, you're right. what was I thinking... :)

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Expert Comment

ID: 2829893
I think that the applet will know to look in the jar file automatically-you dont have to tell it anything because if you include  codebase tag pointing to your jar file it will look in it first then it will look in the directory where the class file is ran from....here is an example

<applet code=Nickel.class codebase="your.jar" width=75 height=30>
<param name=text VALUE="Discriminate">
<param name=sound Value="nickel_disc.au">

Author Comment

ID: 2830417
Thanks okcman,  I will try your suggestion later, but I'm afraid I can't take it as an answer because I might need to have more than one jar file, each jar file may need to have its own images.

jerch, thank you.

Expert Comment

ID: 2831609
you're welcome :)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question