The Artima Developer Community
Sponsored Link

Getting Loaded
Stepping Through a Class File

Advertisement

The Getting Loaded applet, included below, simulates a Java virtual machine loading a class file. This applet accompanies Chapter 6, "The Java Class File," of Inside the Java 2 Virtual Machine.

For some reason, your browser won't let you view this way cool Java applet.

The class file being loaded in the simulation was generated by the javac compiler from the Java source code shown below. Although the snippet of code used in the simulation may not be very useful in the real world, it does compile to a real class file and provides a reasonably simple example of the class file format. This is the same class used in the Eternal Math simulation applet that accompanies Chapter 5, "The Java Virtual Machine."

// On CD-ROM in file classfile/ex1/Act.java
class Act {

    public static void doMathForever() {
        int i = 0;
        for (;;) {
            i += 1;
            i *= 2;
        }
    }
}

The Getting Loaded 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 Java virtual machine. Just press the Step button to cause the virtual machine 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 Java virtual machine 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 Java virtual machine, one chunk at a time. The chunk of bytes to be consumed by the virtual machine on the next Step button press are shown in red. These highlighted bytes are described in the large text area above the Java virtual machine. Any remaining bytes beyond the next chunk are shown in black.

Many items in the class file refer to constant pool entries. To make it easier for you to look up constant pool entries as you step through the simulation, a list of the contents of Act's constant pool is shown here:

Class Act's constant pool
Index
Type
Value
1
CONSTANT_Class_info
7
2
CONSTANT_Class_info
16
3
CONSTANT_Methodref_info
2, 4
4
CONSTANT_NameAndType_info
6, 5
5
CONSTANT_Utf8_info
"()V"
6
CONSTANT_Utf8_info
"<init>"
7
CONSTANT_Utf8_info
"Act"
8
CONSTANT_Utf8_info
"Act.java"
9
CONSTANT_Utf8_info
"Code"
10
CONSTANT_Utf8_info
"ConstantValue"
11
CONSTANT_Utf8_info
"Exceptions"
12
CONSTANT_Utf8_info
"LineNumberTable"
13
CONSTANT_Utf8_info
"LocalVariables"
14
CONSTANT_Utf8_info
"SourceFile"
15
CONSTANT_Utf8_info
"doMathForever"
16
CONSTANT_Utf8_info
"java/lang/Object"

Each chunk of bytes is fully explained in the text area. Because there is a lot of detail in the text area, you may wish to skim through all the steps first to get the general idea, then look back for more details. Happy clicking.


Click here to view a page of links to the source code of the Getting Loaded applet.


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us