Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Copying .class files

Most Web browsers play Java applets when they encounter them. They do not save them onto your hard drive in an easily retrievable form. Downloaded .class files may or may not be present in your browser’s cache. However, whether or not a browser caches a .class file on disk, you can use the following trick to download a copy of the file you want.

Let’s suppose you’ve seen a cool applet on a Web site at http://www.idgbooks.com/example.html and you want to learn how the programmer wrote it. Of course you’ll need a copy of the applet’s .class files. But how do you get them? This will take a little work, but it’s really not hard. Here are the steps.

1.  Use your Web browser’s View Source command to see the HTML for the Web page. You’re looking for the <APPLET> tag like this:
   <applet code=CoolApplet.class width=200 height=200>
   </applet>
2.  Write a very simple Web page that includes an HREF link to the file you want to look at it. For example,
   <A HREF=”http://www.idgbooks.com/CoolApplet.class”>Download Me</a>
3.  Load the page with the HREF link to the file into your Web browser. Then, use the Save this Link As command in the pop-up menu to save the file on your hard drive, as shown in Figure 4-2. That’s it. You should now have a clean copy of the .class file to work with.


Figure 4-2  The Save this Link as menu command in the Macintosh version of Netscape.

Zip files

Most VMs include their class libraries in the form of uncompressed zip archives called classes.zip. If you want to poke around in the innards of the class library, the first thing you need to do is unzip this file.

Theoretically, it shouldn’t matter whether a package hierarchy is or is not zipped, as long as your CLASSPATH is set up properly. In practice, that’s not always true. Before dearchiving someone else’s file for experimentation, you should always copy it to a directory that’s not in your CLASSPATH. It’s best not to work on the original copies.

As soon as the Sysops had installed JDK 1.1 on sunsite.unc.edu, I copied the classes.zip file to a test directory of my home directory and then unzipped it to start poking around. In other words,

   % mkdir ~/test
   % cp /usr/local/java-1.1/lib/classes.zip ~/test
   % unzip ~/test/classes.zip

There are many tools available for unzipping .zip files. Unzip is the dearchiver of choice for zip files on UNIX. StuffIt Expander works well on the Macintosh. Although PKZip is the original zip program, it cannot handle the long filenames that Java requires. Therefore, on Windows platforms, you should use WinZip instead.

Jar files

Java 1.1 introduced Jar files. Jar is a rough concatenation of “Java archive.” Jar files can contain all the .class files, image files, sound files, and other files needed to run an applet. By placing all these different files into a single file, a Web browser can download them with only a single request to the Web server. Depending on the server’s load and network conditions, this can save from a few milliseconds to several minutes of time. Furthermore, a Jar file can compress its contents so the savings can be even larger.

Jar files are included on Web pages with applet tags that look like this:

   <applet code=CoolApplet.class width=200 height=200>
     <param name=archives value=”jars/coolapplet.jar”>
   </applet>

You can download a Jar file to your hard drive exactly the same way you’d download a .class file. Once you have the Jar file on your local hard drive, you need to dearchive it to retrieve the individual parts. The java.util.zip package includes classes that can parse and handle Jar files. Sun’s JDK 1.1 for Windows and Solaris includes a command line jar program based on this package that you can use to pack and unpack Jar files. An equivalent program will likely be available for the Macintosh by the time this book hits store shelves.

The jar command line syntax (see Table 4-6) is deliberately similar to the classic UNIX tar command. Options are passed as one-character flags that follow the word jar on the command line. Archiving versus dearchiving is chosen through the c (create) or x (extract) flag, not via jar and unjar commands as a PKZip user might expect.

Table 4-6 Command line options for jar

Option Purpose

c Creates a new jar archive on System.out.
t Lists the table of contents of the jar file on System.out.
x Extracts from System.in.
f The second argument specifies the jar file to process or create.
v Produces verbose output on System.err.

For example, to archive all files in the current directory you would type

   C:\> jar cf allfiles.jar *

To archive just some files and directories, specify them by name on the command line like this:

   C:\> jar cf allfiles.jar CoolApplet.class Helper.class audio images

Directories are archived recursively; in other words, their immediate contents and the contents of any sub-directories are archived.

To dearchive the file, you would type the following:

   C:\> jar xf allfiles.jar

Summary

In this chapter, you learn about the format of Java .class files and how to read them. In particular, you learn the following:

!  You can view the same program in different formats: pure hexadecimal bytes, ASCII text, disassembled byte code, and Java source code. The first three formats are available in the .class file. The last is the form of the .java source code file.
!  How a .class file is organized and how to split it into its component parts.
!  How to work backward from the compiled .class byte code file to an approximation of the source that generated it. This task will continue in the next chapter.
!  That there are legal issues involved in doing this. Although copyright doesn’t prevent reverse engineering any more than it prevents you from reading a book you’ve bought, it may prevent you from copying what you’ve learned verbatim. Patents may provide more serious restrictions.
!  How to retrieve a .class file, wherever it resides, whether on a Web site, in a zip archive, or in a jar file.

In the next chapter, you learn how to decode and understand method bodies, instead of just printing them as streams of bytes.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.