Solved

Images in jar files

Posted on 2000-05-16
6
185 Views
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.
0
Comment
Question by:mtngu76
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
jerch earned 50 total points
Comment Utility
Hi mtngu76...
URL url = MyClass.getClass().getResource("<dir>/MyImage.jpg");

ImageIcon = new ImageIcon(url);

best regards...
Jerson

0
 

Author Comment

by:mtngu76
Comment Utility
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.


0
 
LVL 4

Expert Comment

by:jerch
Comment Utility
Oh sorry, you're right. what was I thinking... :)

Jerson
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:okcman
Comment Utility
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">
</applet>
0
 

Author Comment

by:mtngu76
Comment Utility
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.
0
 
LVL 4

Expert Comment

by:jerch
Comment Utility
you're welcome :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now