Running Java apps

A standalone Java app must have at least one class that contains a method called main( ), which contains the first code to be executed upon start up. To run the app, start the VM, specifying that class as an argument. You can also specify options to the interpreter as well as arguments to be passed to the app. Sun's Java VM is called java:

 % java  [interpreter options ] class_name  [program arguments ]


The class should be specified as a fully qualified class name, including the package name, if any. Note, however, that you don't include the .class file extension. Here are a couple of examples:

 % java animals.birds.BigBird
 % java MyTest


The interpreter searches for the class in the classpath, a list of directories and archive files where classes are stored. We'll discuss the classpath in detail in the next section. The classpath can be specified either by an environment variable or with the command-line option -classpath. If both are present, the command-line option is used. Alternately, the java command can be used to launch an "executable" Java archive (JAR) file:

 % java -jar spaceblaster.jar


In this case, the JAR file is annotated with the name of the class containing the main( ) method and the classpath becomes the JAR file itself. However it is launched, after loading the first class, the interpreter executes the class's main( ) method. From there, the app can reference other classes, start additional threads, and create its user interface or other structures, as shown in Screenshot-1.

Screenshot-1. Starting a Java app
Java ScreenShot

The main( ) method must have the right method signature. A method signature is the set of information that defines the method. It includes the method's name, arguments, and return type, as well as type and visibility modifiers. The main( ) method must be a public, static method that takes an array of String objects as its argument and does not return any value (void):

 public static void main ( String [] myArgs )


The fact that main( ) is a public and static method simply means that it is globally accessible and that it can be called directly by name. We'll discuss the implications of visibility modifiers such as public and the meaning of static in s 4 through 6. The main( ) method's single argument, the array of String objects, holds the command-line arguments passed to the app. The name of the parameter doesn't matter; only the type is important. In Java, the content of myArgs is a true array. There's no need for an argument count parameter because arrays know how many arguments they contain and can happily provide that information:

 int numArgs = myArgs.length;


myArgs[0] is the first command-line argument, and so on.[*]

[*] Note that this differs from some languages, where argument zero is the name of the app.

The Java interpreter continues to run until the main( ) method of the initial class file returns and until any threads that it starts are complete. Special threads designated as daemon threads are automatically killed when the rest of the app has completed.

System Properties

Although it is possible to read host environment variables from Java, it is generally discouraged for app configuration. Instead, Java allows any number of system property values to be passed to the app when the VM is started. System properties are simply name-value string pairs that are available to the app through the static System.getProperty( ) method. You can use these properties as a more structured and portable alternative to command-line arguments and environment variables for providing general configuration information to your app at startup. Each system property is passed to the interpreter on the command line using the -D option followed by name=value. For example:

 % java -Dstreet=sesame -Dscene=alley animals.birds.BigBird


The value of the street property is then accessible this way:

 String street = System.getProperty("street");


An app can get its configuration in myriad ways, including via files or network configuration at runtime. In practice, Java apps make heavy use of system properties for basic configuration.

Comments