Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Technical Tips

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 
Tech Tips index

Tech Tips
September 3, 1997

This first issue of the Java Developer ConnectionTM Tech Tips presents tips, techniques, and sample code for the following topics:

JAR Files
Have you tried using JAR files for archiving? You may have heard of a UNIX tool called "tar," or tape archiver, that's used to group files together for backup purposes. In the Java Development Kit 1.1 and later releases, there's a similar facility known as "jar," or Java Archiver. Here's a way to use this tool.

A JAR archive is created with the following:


     $ jar cf archive.jar file1 file2 ...

After you create the archive, you can list the contents with:


     $ jar tf archive.jar

and files can be extracted with the following:


     $ jar xf archive.jar [file1 file2 ...]

Why is the JAR facility important? After all, various ZIP tool versions exist for archiving and compressing files. One important use of JAR is for bundling Java .class files. A group of such files could constitute a Java package or library. For example, with Windows 95 or NT you might say:


        CLASSPATH="lib1.jar;lib2.jar;"

Another very important JAR use is for optimizing applet loading. Consider this example with a simple applet:


// applet.java
import java.awt.*;
import java.applet.*;

public class applet extends Applet {
  public void paint(Graphics g)
  {
    String s = Message.getIntro(getParameter("intro"));
    g.drawString(s, 25, 25);
  }
}

that accesses an auxiliary class:


     // Message.java
     public class Message {
         public static String getIntro(String t)
         {
             if (t != null)
                 return t;
             else
                 return "Hello world!";
         }
     }

You can invoke this applet with some HTML code, such as:


<HTML>
<HEAD>
<TITLE>Hello World Example Applet </TITLE>
</HEAD>
<BODY>
<APPLET CODE="applet.class" ARCHIVE="applet.jar" 
  WIDTH="150" HEIGHT=150>
<PARAM NAME="intro" VALUE="good morning">
</APPLET>
</BODY>
</HTML>

Note the "archive" attribute. Without this setting, each subsidiary class that's loaded, such as Message, would involve a separate request to the server holding the HTML page. But with the JAR file, the various .class files can be downloaded more efficiently.

In this example, you prepare archive.jar with the following:


     $ javac applet.java
     $ jar cf applet.jar *.class

that is, construct the JAR file from all of the .class files.

A final note: JAR files are also used with JavaBeans.

Performance tip: Garbage Collection and setting to null
Java uses garbage collection, or reclaiming no-longer-used storage, rather than requiring you to explicitly manage storage. Garbage collection is automatic, but sometimes there are ways to help it out. Imagine a case where you're managing a stack of Object references:


public class Stack {
  private static final int MAXLEN = 10;
  private Object stk[] = new Object[MAXLEN];
  private int stkp = -1;

  public void push(Object p) {stk[++stkp] = p;}

  public Object pop() {return stk[stkp--];}
}

Now consider a case where the stack has two elements on it, and you pop one of them. At this point stk[0] will have a valid element in it, and stk[1] will have the element just popped. That is, stk[1] will have a reference to an Object, which could be a reference to anything, including a large data structure of many thousands of bytes. In such a case, this data structure cannot be garbage collected, even though it may no longer be in use.

To remedy this problem, you can rewrite pop like so:


     public Object pop()
     {
         Object p = stk[stkp];
         stk[stkp--] = null;
         return p;
     }

You haven't nullified the Object itself, just a reference to it that's no longer valid. The Stack object itself may have a long lifetime, and rewriting the pop method in this way helps ensure that garbage collection gets done in a timely fashion.


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.