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... :)

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month6 days, 22 hours left to enroll

592 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