Solved

images in jar

Posted on 1998-07-05
3
223 Views
Last Modified: 2013-11-23
Hi,

I am using applet which loads lots of small gifs.
I tried to archive the gifs with the class files, but when
 my applet tried loading them from the jar file , i got an exception.

Is there a way to jar also the images my applet uses ?

thanks,
Avi Kavas.
0
Comment
Question by:batat
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 50 total points
Comment Utility
Hi Avi. The answer divides to two. If you are using IE4 than you can jar all the classes and images together. You then open each image through url as if its not in a jar, and the IE4 caches them for you and read them from the jar. You can even use the very helpfull reasource management command Class.getReasource . In Netscape the situation is a bit difficult. The caching is not done for you if you are not using the java plug-in or the scpecial Netscape java1.1 patch. You can pack the images in a jar but read the image from the jar over the net (less efficient). If you need this sort of code I can note in a sample that does that. It will work seamlesly for Netscaep and IE4.
Don't hesitate to ask for more info.
G.
0
 

Author Comment

by:batat
Comment Utility
Can you please give me the source code of your suggestions.

thanks,
Avi.

0
 
LVL 6

Expert Comment

by:gadio
Comment Utility
Shalom Avi.

Heres the code you can use:

import java.applet.Applet;
import java.awt.*;
import java.net.URL;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import rsctst.*;

public class resource_test extends Applet {

  /**
   * Initializes the applet.  You never need to call this directly; it is
   * called automatically by the system once the applet is created.
   */
  public void init() {
        setLayout( new FlowLayout());
        add(new TextField("images/image.gif"));
        rs r = new rs();
        InputStream rs_stream = null;
        rs_stream = getResourceInputStream( r.getClass(), "images/image1.gif" );
        System.out.println( rs_stream );
        System.out.println( getParameter("archive") );
        System.out.println( getParameterInfo() );
  }

  /**
   * Called to start the applet.  You never need to call this directly; it
   * is called when the applet's document is visited.
   */
  public void start() {
  }

    public InputStream getResourceInputStream( Class c, String resource ) {
        String archive;
        InputStream result_stream = null;
        String name = c.getName();
        String path = new String();
        String item = new String();
        StringTokenizer st = new StringTokenizer( name, "." );
        URL resulturl = null;
        while( st.hasMoreElements() ) {
            path+=item;
            item = (String)st.nextElement()+"/";
        }
        if( (archive = getParameter("archive")) == null ) {
            try {
              resulturl = new URL(getCodeBase(),path+resource);
              result_stream = resulturl.openStream();
            } catch( Exception e ) {
                System.out.println("E:"+e);
            }
        }
        else {
            try {
              resulturl = new URL(getCodeBase(),archive);
              InputStream in = resulturl.openStream();
              ZipInputStream zin = new ZipInputStream(in);
              ZipEntry ze;
              while( !((ze = zin.getNextEntry()).getName()).equals(path+resource));
              result_stream = resulturl.openStream();
            } catch( Exception e ) {
                System.out.println("Ex:"+e);
            }
        }
        return result_stream;
    }
}




Note that what you need is the function getReasourceInputStream, through which you read the image. The reasource will always be looked in relation to the location of class c that you send. That enables you to put all the imges in a centeral archive or with the relevent classes.
Good luck,
Gadi.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

772 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

9 Experts available now in Live!

Get 1:1 Help Now