Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

images in jar

Posted on 1998-07-05
3
Medium Priority
?
232 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

715 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