Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

The Java class file lifestyle

An introduction to the basic structure and lifestyle of the Java class file

  • Print
  • Feedback

Page 4 of 4



Attributes

Bringing up the rear are the attributes, which give general information about the particular class or interface defined by the file. The attributes section has a two-byte count of the number of attributes, followed by the attributes themselves. For example, one attribute is the source code attribute; it reveals the name of the source file from which this class file was compiled. JVMs will silently ignore any attributes they don't recognize.



Getting loaded: a simulation of a class file reaching its JVM destination

The applet below simulates a JVM loading a class file. The class file being loaded in the simulation was generated by the javac compiler given the following Java source code:



class Act {
    public static void doMathForever() {
        int i = 0;
        while (true) {
            i += 1;
            i *= 2;
        }
    }
}


The above snippet of code comes from last month's article about the JVM. It is the same doMathForever() method executed by the EternalMath applet from last month's article. I chose this code to provide a real example that wasn't too complex. Although the code may not be very useful in the real world, it does compile to a real class file, which is loaded by the simulation below.



The GettingLoaded applet allows you to drive the class load simulation one step at a time. For each step along the way you can read about the next chunk of bytes that is about to be consumed and interpreted by the JVM. Just press the "Step" button to cause the JVM to consume the next chunk. Pressing "Back" will undo the previous step, and pressing "Reset" will return the simulation to its original state, allowing you to start over from the beginning.



The JVM is shown at the bottom left consuming the stream of bytes that makes up the class file Act.class. The bytes are shown in hex streaming out of a server on the bottom right. The bytes travel right to left, between the server and the JVM, one chunk at a time. The chunk of bytes to be consumed by the JVM on the next "Step" button press are shown in red. These highlighted bytes are described in the large text area above the JVM. Any remaining bytes beyond the next chunk are shown in black.



I've tried to fully explain each chunk of bytes in the text area. There is a lot of detail, therefore, in the text area and you may wish to skim through all the steps first to get the general idea, then look back for more details.



Happy clicking.



You need a Java-enabled browser to view this applet.



Click here for the source code of GettingLoaded. To run this applet on your own, you'll also need the two files that this applet retrieves from the server, the ASCII file that contains the text for each step and the Act.class file itself. Click here for the source code of the Flying Class Files audio applet.



ENDNOTE: The small print: "The Java Class File Lifestyle" Article Copyright (c) 1996 Bill Venners. All rights reserved. "GettingLoaded" Applet Copyright (c) 1996 Artima Software Company. All rights reserved.

About the author

Bill Venners is president of Artima Software Company. Through Artima, he does custom software development and consulting.:END_ENDNOTE

  • Print
  • Feedback

Resources
  • The Java Virtual Machine Specification, the official word from Sun.
    http://java.sun.com/1.0alpha3/doc/vmspec/vmspec_1.html
  • When it comes out, the book The Java Virtual Machine Specification, http://www.aw.com/cp/lindholm-yellin.html, by Tim Lindholm and Frank Yellin (ISBN 0-201-63452-X), part of The Java Series, http://www.aw.com/cp/javaseries.html), from Addison-Wesley, will likely be the best JVM resource.
  • A draft of chapter 4 of The Java Virtual Machine Specification, which describes the class file format and bytecode verifier, can be retrieved from JavaSoft.
    http://java.sun.com/java.sun.com/newdocs.html