Images in jar files

Posted on 2000-05-16
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

jerch earned 50 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 Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.


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 is an example

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

717 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