Access images from jar

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>


agneskoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

agneskoAuthor Commented:
Edited text of question.
0
sen_kumCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.