Solved

images in jar

Posted on 1998-07-05
3
230 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
[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
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 50 total points
ID: 1224588
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
ID: 1224589
Can you please give me the source code of your suggestions.

thanks,
Avi.

0
 
LVL 6

Expert Comment

by:gadio
ID: 1224590
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.

729 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