Previous Section  < Day Day Up >  Next Section

The java Interpreter

java, the Java interpreter, is used to run Java applications from the command line. It takes as an argument the name of a class file to run, as in the following example:


java BidMonitor


Although Java class files end with the .class extension, this extension will not be specified when using the interpreter.

The class loaded by the Java interpreter must contain a class method called main() that takes the following form:


public static void main(String[] arguments) {

    // Method here

}


Some simple Java programs might consist of only one class—the one containing the main() method. In more complex programs that make use of other classes, the interpreter automatically loads any other classes that are needed.

The Java interpreter runs bytecode—the compiled instructions that are executed by a Java virtual machine. After a Java program is saved in bytecode as a .class file, it can be run by different interpreters without modification. If you have compiled a Java 2 program, it will be compatible with any interpreter that fully supports Java 2.

By the Way

Interestingly enough, Java is not the only language that you can use to create Java bytecode. NetRexx, JPython, JRuby, JudoScript, and several dozen other languages will compile into .class files of executable bytecode through the use of compilers specific to those languages. Robert Tolksdorf maintains a comprehensive list of these languages that is currently available from the web page at http://www.robert-tolksdorf.de/vmlanguages.


There are two different ways to specify the class file that will be run by the Java interpreter. If the class is not part of any package, you can run it by specifying the name of the class, as in the preceding java BidMonitor example. If the class is part of a package, you must specify the class by using its full package and class name.

For example, consider a SellItem class that is part of the org.cadenhead.auction package. To run this application, the following command would be used:


java org.cadenhead.auction.SellItem


Each element of the package name corresponds to its own subfolder The Java interpreter will look for the SellItem.class file in several different places:

  • The org\cadenhead\auction subfolder of the folder where the java command was entered (if the command was made from the C:\J24work folder, for example, the SellItem.class file can be run successfully if it was in the C:\J24work\org\cadenhead\auction folder)

  • The org\cadenhead\auction subfolder of any folder in your CLASSPATH setting

If you're creating your own packages, an easy way to manage them is to add a folder to your CLASSPATH that's the root folder for any packages you create, such as C:\javapackages or something similar. After creating subfolders that correspond to the name of a package, place the package's class files in the correct subfolder

Java supports assertions, a debugging feature that only works when requested as a command-line option. To run a program using the Java interpreter and make use of any assertions that it contains, use the command line -ea, as in the following example:


java -ea Outline


The Java interpreter executes all assert statements in the application's class and all other class files that it uses, with the exception of classes from the Java class library.

To remove that exception and make use of all assertions, run a class with the -esa option.

If you don't specify one of the options that turns on the assertions feature, all assert statements will be ignored by the interpreter.

    Previous Section  < Day Day Up >  Next Section