![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Copying .class filesMost 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 browsers 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. Lets suppose youve 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 youll need a copy of the applets .class files. But how do you get them? This will take a little work, but its really not hard. Here are the steps.
Zip filesMost 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 shouldnt matter whether a package hierarchy is or is not zipped, as long as your CLASSPATH is set up properly. In practice, thats not always true. Before dearchiving someone elses file for experimentation, you should always copy it to a directory thats not in your CLASSPATH. Its 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 filesJava 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 servers 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 youd 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. Suns 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.
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 SummaryIn this chapter, you learn about the format of Java .class files and how to read them. In particular, you learn the following:
In the next chapter, you learn how to decode and understand method bodies, instead of just printing them as streams of bytes.
|
![]() |
|