Command Line Arguments

Our Hello program still isn’t very general. We can’t change the name we say hello to without editing and recompiling the source code. This may be fine for the programmers, but what if the secretaries want their computers to say Hello to them? (I know. This is a little far-fetched but bear with me. I’m making a point.)

What we need is a way to change the name at runtime rather than at compile time. (Runtime is when we type java HelloRusty. Compile time is when we type javac HelloRusty.java). To do this we’ll make use of command-line arguments. They allow us to type something like Java Hello Gloria and have the program respond with “Hello Gloria”. Here’s the code:

// This is the Hello program in Java
class Hello {

    public static void main (String args[]) {

      /* Now let's say hello */
      System.out.print("Hello ");
      System.out.println(args[0]);
  }

}

Compile this program in the javahtml directory as usual and then type java Hello Gloria.

This isn’t very hard is it? In fact we’ve even gotten rid of the name variable from the HelloRusty program. We’re using args[0] instead. args is what is known as an array. An array stores a series of values. The values can be Strings as in this example, numbers, objects or any other kind of Java data type.

args is a special array that holds the command line arguments. args[0] holds the first command line argument. args[1] holds the second command line argument, args[2] holds the third command line argument and so on.

At this point almost everyone reading this is probably saying “Whoa, that can’t be right.” However why you’re saying depends on your background.

If you’ve never programmed before or if you’ve programmed only in Pascal or Fortran, you’re probably wondering why the first element of the array is at position 0, the second at position 1, the third at position 2 instead of the clearly more sensible element 1 being the first element in the array, element 2 being the second and so on. All I can tell you is that this is a holdover from C where this convention almost made sense.

On the other hand if you’re used to C you’re probably upset because args[0] is the first command line argument instead of the command name. The problem is that in Java it’s not always clear what the command name is. For instance in the above example is it java or Hello? On some systems where Java runs there may not even be a command line, the Mac for example.

Now you should experiment with this program a little. What happens if instead of typing java Hello Gloria you type java Hello Gloria and Beth? What if you leave out the name entirely, i.e. java Hello?

That was interesting wasn’t it? You should have seen something very close to

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException at Hello.main(C:\javahtml\Hello.java:7)

What happened was that since we didn’t give Hello any command line arguments there wasn’t anything in args[0]. Therefore Java kicked back this not too friendly error message about an “ArrayIndexOutOfBoundsException.” That’s a mouthful. We’ll see one way to fix it in the next section.

Comments are closed.