[   ^ to index...   |   <-- previous   |   next -->   ]

Java environment and tools

Compiling

In Sun's implementation of Java, the Java compiler is named javac:

    javac Foo.java

The output will be a classfile for the class(es) in the file, plus any classes that the file depends on---Java automatically tries to process dependencies. The classfile name, by convention, has the .class extension, so the output of the above would be a file named "Foo.class".

The Java Virtual Machine

You cannot execute classfiles directly. Instead, the Java compiler produces code for the "Java Virtual Machine". The instructions of this virtual machine are called "bytecode". Think of bytecode as machine code for the virtual machine, just as a compiled C executable is machine code for your physical machine.

What is a "virtual machine"? It is simply a precisely specified abstract computer, which usually does not exist in hardware. To run code for a virtual machine on platform X (x86 Windows, PowerPC Mac, whatever), you write software that implements the virtual machine on platform X. Hence, Java bytecode compiled on any platform can theoretically run unchanged on any other platform for which a JVM implementation exists.

There is some overloading of the phrase "virtual machine": it commonly refers both to

So, if you want to run a classfile, you should invoke the Java virtual machine on the bytecode file. In Sun's implementation, the virtual machine executable is named java or jre:

    java Foo

Notice the lack of .class extension. This is a convention of JVMs: you specify the class name, not a classfile name, and it is the JVM's job to go and find the file(s) corresponding to that class. (There's a good reason for this---the Java package system, which we'll discuss shortly.)

Virtual machines vs. interpreters

Having played with the SML/NJ interpreter/compiler and the Smalltalk virtual machine, you are probably wondering: What is the difference between an interpreter and a virtual machine?

My answer would be: When people say "a virtual machine", they are implying that it runs an intermediate language (like Java bytecode) that has a rigid specification---which enables all compliant virtual machines to run the same compiled program. People often assume interpreters run more slowly than virtual machines, but there's nothing about the definition of an interpreter that says it cannot be implemented using an optimizing compiler---like SML/NJ.


Keunwoo Lee
Last modified: Wed May 23 22:21:16 PDT 2001