Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Programming =>  Java

Command Line Arguments

  buildingblocksjava.c      2011-09-30 11:31:54      1,367    0    0

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 To do this we’ll make use ofcommand-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 ");


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 usingargs[0]instead.argsis 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.

argsis 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 becauseargs[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\

What happened was that since we didn’t give Hello any command line arguments there wasn’t anything inargs[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.

Source :



Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



No comment for this article.


Random number generator

By sonic0002
Image authorized by Sina Weibo user 爱开源魅影