Department of Computer Science
Version 3.2.0, January, 2010
If you are still using Java 1.5, download Version 3.1 .
Every other CPU has a machine level debugger/viewer. Why not one
for the Java Virtual Machine?
The JVMViewer simulates JVM (Java Virtual Machine)
instructions of a method in a class. It will show you how each
instruction in a method is interpreted. Well almost. Please
review the caveats below.
Its primary purpose is to debug simple programs written with the
to show what actually is happening. It also shows the execution
of (most) methods of a compiled Java program.
- Installing and running the JVMViewer
- Loading a class file
- Selecting a method
- Selecting parameters
- Stepping through
- References, source code
Installing and running the JVMViewer
- Download jvmem.jar and save it in the directory
where you normally keep your .class
Be sure that file is saved as jvmem.jar. Some
browsers/OS's will want to save it as jvmem.zip.
If that happens, just rename it to jvmem.jar.
- In Windows, or Unix/Linux,
the directory containing your .class files. Start the
JVMViewer by entering
The JVMViewer is found in the jvmem
package contained in the jvmem.jar
The window should look like
Problems starting up?
This program needs Java 1.6.0. If you are still using 1.5.0,
download version 3.1
- You can start the JVMViewer in other ways, depending upon your
You can change the size of the display font by selecting Zoom
Text or Unzoom Text from the Help submenu.
- For the impatient, you can load a class file from the
command line by, for example, java -jar jvmem.jar -f
c:\PathToJavaClass\AJavaClass. (In Unix, the slashes are
- For the person who is lazy: In the file listing window,
double-click on the
icon. However, you will not be able to see the output to the
- For the adventuresome who does not like to type: Copy jvmem.jar
to the directoryContainingTheJRE\lib\ext
-- in Unix use the forward rather than the backslash --
subdirectory. Then all you have to do is
to start up the JVMViewer.
Loading a class
In the File submenu, Open the .class file that you want to
view. You can browse to any directory that
contains a .class
file. While clicking on a .class
file icon is preferred, you
can type in a class file name with or without the .class suffix.
guarantee that a jasmin
is well behaved, and our interpreter assumes that it is, this step is
necessary. (Adding a float and an integer is not a good
idea.) The display will look like
If you want to to load a class in a package,
you must first get
to the directory where the package subdirectory is located. Then
type in packageName.ClassName.
you can execute java
to load this class, you would enter mypackage.MyClass for the file
name while in the
(For the Mac, you'll have to load the file using the -f
command line option
for example: java -jar jvmem.jar -f mypackage.MyClass.)
You can see all the methods, the constant pool, and a hexadecimal dump
of the class by selecting Show Class from the Method submenu.
From the Method submenu, select a
method. You can distinguish between static methods and instance
methods, since the latter are highlighted. The screen will then
the object code, the stack size, and the maximum number of local
variables. This is shown in the beginning of the first
- Instance methods must have an object associated with it.
The JVMViewer will create an object; it will be passed in as the first
parameter. Note: at this time, objects must have the default
constructor (the one with no parameters).
- Required data for the parameters, if any, can be passed by
selecting from the
Parameters submenu. A dialog will prompt you for the type of
- For the primitive
types int, char, float, long, double and boolean, you should have no
problem passing in data. (The other
primitive types can be easily implemented. But why?)
- For the String
type, just type in the text box. Note that pressing OK without
entering any data will be the empty string "", not null. If you want to
input null, just type null.
- For object types, a message will confirm
that the object
has been created. That is, if the class for the object has the
default constructor (the one with no parameters). It will
complain if it cannot create such an
object. Thus, we cannot interpret methods that pass an File object (at least for
- Arrays of int,
char, float, String, and AnyObject can
also be passed as parameters. An array of strings will be single
words. For an array of objects, a dialog box will ask for the
size of the array. The class for the object must have the default
constructor. Pressing OK without entering any array data will create an
array with 0 elements. If you want to input null, just type null.
The String parameter
allows us to execute main
methods. Try it.
- Once all the parameters have been filled in, the
interpreter will begin execution. The display will be similar to
the first figure. The program counter (pc)
will be a at location 0 of the instructions. The instruction to
be executed is displayed. The stack (any data
above ----------------) will
local variables. Integers and floats are displayed as such.
Characters and booleans are converted to their integer values.
Longs and doubles take up two local variables; the higher variable
value is shown as --.
References to arrays and objects are shown as -> array/object. Any
local variable or stack value that is either undefined or contains null is shown as --.
The JVMViewer will complain if it cannot pass a particular
- Press the Step button to execute a machine instruction.
After execution, the next instruction to be executed is shown.
The stack will be shown above above ---------------- and the values
of the local variables will be shown below.
- The default is to execute one machine instruction at a time,
though you may change that value. In particular you can choose one
another in which the interpreter continuously executes an
instruction about once per second. You can stop this by changing
the step size to a number; in particular, selecting Execute no instructions will
- You can set a breakpoint by entering -> <n>
where <n> is the location of an instruction. For
example, if you want to execute a program up to the instruction at
location 100, enter -> 100. After pressing Step, the
JVMViewer will execute instructions up to the breakpoint. Though you
can set many breakpoints, only the one that is displayed will be active.
- If a method calls another method in the same class, you can Step
into the called method. Another window will open with that method.
(This can be fun when doing recursion.)
- When the method completes by one of the returns, the interpreter will
show the value returned, if any. The null value is shown as --
- Most, but not all, instructions can be interpreted.
To see which ones that aren't, run
This will list the unimplemented instructions, such as the ever popular
- If a method needs an object, say MyObject, then MyObject
must have the null (MyObject()) constructor.
- Some "unusual" cases are not yet interpreted.
For example, calling a static method with a short parameter.
The JVMViewer will let you know about this.
- Interpreting the constructors (init methods) may be
- This all started with the desire to write assembly code for the
Java Virtual Machine. Looking at architectures may be fine, but
writing code at a low level is an eye-opener.
- For the assembler, look at the jasmin site, or
download jasmin-1.1.zip here.
overview of the machine instructions from the Jasmin site. Detailed
explanations of the machine instructions from Oracle's site. The original
(now archived) Jasmin reference has a better interface.
- Learning to write simple code for the JVM is easy. Take a
look at my tutorial.
for the course that I taught at Montclair State.
- Source code, in a
files are in the package jvmem,
create javadocs, if you wish. You'll find interesting
comments. (You can see them in an older Version 2.1.1.)
Carl E. Bredlau
College of Science and
1 Normal Avenue
Montclair, NJ 07043