By sundararajan on Mar 19, 2007
Recently, I gave a series 4 talks (consecutive saturdays about 2 to 2.5 hours) at CS department of Anna University on Java Virtual Machine. The aim was to give understanding JVM at specification level and not much on any specific implementation of it. I did not create slides on my own. Also, I didn't stick only to slides - I did mostly marker-pen-and-board as well.
Initial reading to introduce the concept of virtual machines:
- First chapter of Virtual Machines: Versatile Platforms for Systems and Processes book.
- P-code machine for Pascal
- javap - comes with JDK. Run with -p (private as well) and -c (code) options.
- jclasslib Bytecode Viewer - very nice "visual" bytecode viewer. Allows us to nativagte various elements in a .class file by hyperlinks. There is also a NetBeans module available for jclasslib.
- Virtual Execution Environments - Jon Vitek
- Artima.com's http://www.artima.com/java/ - has very nice illustrative applets showing various aspects of the JVM
Writing code using a Java assembler gives very good understanding. I used jasmin assembler to show various JVM instructions. Also, we can see JVM's bytecode verifier in action:
You can assmeble the above code by the command
.class public Adder .super java/lang/Object ; standard constructor .method public <init>()V aload_0 invokespecial java/lang/Object/
()V return .end method .method public static main([Ljava/lang/String;)V .limit stack 2 .limit locals 2 ; nothing on stack, try using "iadd" iadd return .end method
java -jar jasmin.jar Adder.jWhen you run the "Adder" class, you get the following verifier error:
Exception in thread "main" java.lang.VerifyError: (class: Adder, method: main signature: ([Ljava/lang/String;)V) Unable to pop operand off an empty stackThere are also Java libraries to work with .class files:
Both the libraries can be used to read/analyze existing .class files and generate/modify .class files.
BCELifer class can be supplied with a .class file. BCELifier generates a Java program to generate the given .class file. By looking at the generated program, we can learn how to use BCEL API.
Command to generate a Java source program using BCELifier for a given "HelloWorld.class" (The generated Java program can be compiled and run to (re-)create HelloWorld.class).
java -cp bcel-5.1.jar org.apache.bcel.util.BCELifier HelloWorld.class
ASM library also comes with similar ASMifierClassVisitor class to generate Java source.
Tutorials on BCEL and ASM:
java -cp $ASM_HOME/lib/asm-util-3.0.jar:$ASM_HOME/lib/asm-3.0.jar org.objectweb.asm.util.ASMifierClassVisitor HelloWorld.class