Solved

Images in jar files

Posted on 2000-05-16
6
188 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
ID: 2816002
Hi mtngu76...
URL url = MyClass.getClass().getResource("<dir>/MyImage.jpg");

ImageIcon = new ImageIcon(url);

best regards...
Jerson

0
 

Author Comment

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


0
 
LVL 4

Expert Comment

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

Jerson
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Expert Comment

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

Author Comment

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

Expert Comment

by:jerch
ID: 2831609
you're welcome :)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
object oriented programming comparison 5 69
tomcat not starting 6 45
hibernate insert example 13 28
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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