?
Solved

How to read images from applet JAR file

Posted on 2001-09-06
6
Medium Priority
?
259 Views
Last Modified: 2013-11-23
I have an applet whose required files (class, gif etc) are stored in a JAR file. I want to read some images from the JAR file to be used as button icons in the applet. I wrote a method to read from the JAR file and to return the image in a ImageIcon object:

public static ImageIcon loadIcon(String name) {
   try {
      ImageIcon imageIcon = new ImageIcon(ClassLoader.getSystemResource(name));
      return imageIcon;
   } catch (Exception exception) {}
      return null;
}

However, it doesn't work. I have also tried adding "/" in front of "name" but still won't work. Any help is appreciated.

BTW, if I run my program as a stand-alone application, the images are read successfully from the JAR file.
0
Comment
Question by:yongsing
[X]
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
6 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6460500
did you try

ImageIcon imageIcon = new ImageIcon(ClassLoader.getResource(name));
0
 
LVL 92

Expert Comment

by:objects
ID: 6462490
Use:

ImageIcon imageIcon =
 new ImageIcon(getClass().getResource(name));

0
 
LVL 9

Author Comment

by:yongsing
ID: 6462658
heyhey_, ClassLoader.getResource() is a non-static method. You can't call it from a static context.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 6462771
getClass().getResource(name) will work in an applet.
0
 
LVL 9

Author Comment

by:yongsing
ID: 6462856
Yes, I've tried it and it works. Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 6462871
no worries.
Thanks for the points :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

765 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