[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Access images from jar

Posted on 2000-01-20
2
Medium Priority
?
180 Views
Last Modified: 2010-04-01
I am writing an applet using JDK1.2 and found problem in accessing the images from the jar archives.
My program behaves normally without using jar. If using jar, all functions behave normally except displaying images.
How can I solve this problem?

Here are the details of my program:

The directory structure of my files like this:
\*.class
\images\*.jpg

I use this command to create the archives:
jar cvf all.jar *.class .\images\

I use this to extract the files and found that the result directory structure is the same as I have described.
jar xf all.jar

In my program, i use this to get the image.
Image image = applet.getImage(getDocumentBase(), "./images/" + imageName);

My html files contain this:
<APPLET CODE=Applet.class ARCHIVE=all.jar WIDTH=600 HEIGHT=400></APPLET>


0
Comment
Question by:agnesko
2 Comments
 

Author Comment

by:agnesko
ID: 2371625
Edited text of question.
0
 
LVL 1

Accepted Solution

by:
sen_kum earned 40 total points
ID: 2371833
To load the gif from jar in an application you must do the following

                try {
                  MediaTracker m = new MediaTracker(this);
                  InputStream is = getClass().getResourceAsStream("Image.gif");
                  BufferedInputStream bis = new BufferedInputStream(is);
                  byte[] bytes = new byte[10000];
                  int byteRead = bis.read(bytes,0,10000);
                  img =   Toolkit.getDefaultToolkit().createImage(bytes,0,byteRead);
                  m.addImage(img, 0);
                  m.waitForAll();
                  }
                 catch(Exception e) {
                  e.printStackTrace();
                  }

                    Because of some security reason, it's not possible with some browser (like
                Netscape) to use the getResource() method from an Applet. Instead we must use
                the getResourceAsStream method.
                   
                try {
                  MediaTracker m = new MediaTracker(this);
                  InputStream is =   getClass().getResourceAsStream("Image.gif");
                  BufferedInputStream bis = new BufferedInputStream(is);
                  byte[] bytes = new byte[10000];
                  int byteRead = bis.read(bytes,0,10000);
                  img = Toolkit.getDefaultToolkit().createImage(bytes,0,byteRead);
                  m.addImage(img, 0);
                  m.waitForAll();
                  }
                 catch(Exception e) {
                  e.printStackTrace();
                  }
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

607 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