X

Sundararajan's Weblog

  • Java
    March 19, 2007

JVM Talks...

Guest Author

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:
Because we want to study a machine, it is useful to look at disassembled code.
I used the following to show disassembled .class files:
  • 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.

I used/referred the teaching materials from these sites/courses as well:

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:


.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

You can assmeble the above code by the command
  java -jar jasmin.jar Adder.j

When 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 stack

There 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.


java -cp $ASM_HOME/lib/asm-util-3.0.jar:$ASM_HOME/lib/asm-3.0.jar org.objectweb.asm.util.ASMifierClassVisitor HelloWorld.class

Tutorials on BCEL and ASM:

Join the discussion

Comments ( 2 )
  • Eugene Kuleshov Monday, March 19, 2007
    BTW, there will be ASM talk at the JavaOne this year. See some more details in my blog at http://jroller.com/page/eu?entry=asm_at_the_javaone_this
  • A. Sundararajan Tuesday, March 20, 2007
    Hi Eugene Kuleshov: Thanks for that info! Looking forward to attending your talk @ JavaOne.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.