Java Program Invocation and Command-Line Arguments

A Java program is started by specifying the name of a Java class on the command-line. For example, let's say you have a class like this:

    public class MyProgram {
        ...

        // The entry-point for my program.
        public static void main(String[] args) {
            ...  // My program's code.
        }
    }

(This code would be stored in MyProgram.java, and compiled by running javac MyProgram.java.)

To run this Java code, you would invoke the Java Virtual Machine (JVM) with this command:

    java MyProgram

The JVM would then load MyProgram.class, and begin executing your program with the main method. This is your program's entry-point.

The main method must be declared in a specific way for the JVM to recognize it. It must be declared public and static, and it must take a single String[] argument and return no value (void). If your class doesn't have a main method with these exact characteristics, then you will get an error when you try to start your Java program:

    java MyProgram
    Exception in thread "main" java.lang.NoSuchMethodError: main

If you see this error, make sure to check whether you have correctly declared your main method. The example code above declares the main method properly.

Command-Line Arguments

Frequently, a Java program needs to handle arguments specified on the command-line. For example, you might want to run your program like this:

    java MyProgram 1234 www.caltech.edu "olive festival"

Somehow, your program needs to be able to access these values from the command-line. The way your program can do this is through the String[] argument passed to your main method. Note that the argument is an array of strings. Each element of the array contains one of the values specified on the command-line.

In the above example, the args array will contain the following values:

Java program arguments are separated by spaces and tabs, except when an argument is enclosed by double-quotes. For the above command-line invocation, the first two arguments are "1234" and "www.caltech.edu", since the Java VM will divide the argument list based on whitespace. The third argument, "olive festival", is not broken into two separate arguments, since it is enclosed with double-quotes.

Note that leading and trailing whitespace characters are removed from the values stored in the args array. Also, for arguments enclosed with double-quotes, note that the double-quotes are removed.

(If you are used to programming in C or C++, you should also note that there is a difference between how arguments are passed to C and C++ programs, vs. how they are passed to Java programs. In C and C++, argv[0] is the name of the program itself, and argv[1] is the first command-line argument to the program. In Java, args[0] is the first command-line argument to the program, and the name of the program itself is not available.)

Numeric Command-Line Arguments

If a Java program requires numeric command-line arguments, it must explicitly convert the string version of the number into the actual numeric value. With the above example, the first argument is "1234", a String, and it must be converted into an actual integer value. The classes in the java.lang package can be used to handle these conversions. For example, to convert args[0] to its integer value, you can write this:
    public static void main(String[] args) {
        int num = 0;

        ...

        try {
            // Parse the string argument into an integer value.
            num = Integer.parseInt(args[0]);
        }
        catch (NumberFormatException nfe) {
            // The first argument isn't a valid integer.  Print
            // an error message, then exit with an error code.
            System.out.println("The first argument must be an integer.");
            System.exit(1);
        }

        ...  // Code that uses the command-line arguments.
    }
    

There are several important points about this code:

Here is a partial list of the primitive data-types, and functions you can use for parsing command-line arguments of those types. These classes are in the java.lang package, so they are available without needing to specify any import statements in your Java programs. Also, the parsing functions are static, so you don't need to create any new objects to use these functions.

Primitive Data Type Parsing Function
boolean boolean Boolean.parseBoolean(String)
int int Integer.parseInt(String)
long long Long.parseLong(String)
float float Float.parseFloat(String)
double double Double.parseDouble(String)

Copyright (C) 2008, California Institute of Technology.
Last updated February 12, 2008.