Section 3.2. Running Java Applications

3.2. Running Java Applications

A standalone Java application 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 application, 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 application. 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 application can reference other classes, start additional threads, and create its user interface or other structures, as shown in Figure 3-1.

Figure 3-1. Starting a Java application

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 Chapters 4 through 6.

The main( ) method's single argument, the array of String objects, holds the command-line arguments passed to the application. 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 application.

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 application has completed.

3.2.1. System Properties

Although it is possible to read host environment variables from Java, it is generally discouraged for application configuration. Instead, Java allows any number of system property values to be passed to the application when the VM is started. System properties are simply name-value string pairs that are available to the application 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 application 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 application can get its configuration in myriad ways, including via files or network configuration at runtime. In practice, Java applications make heavy use of system properties for basic configuration.

    Learning Java
    Learning Java
    ISBN: 0596008732
    EAN: 2147483647
    Year: 2005
    Pages: 262

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: