Command Line Argument in java

Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main( ). A command-line argument is information that directly follows the program’s name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy — they are stored as strings in a String array passed to the args parameter of main( ). The first command-line argument is stored at args[0], the second at args[1], and so on.
For example,

/* JAVA program to display all command-line arguments */
/* www.codewindow.in */
class codewindow
{
    public static void main(String args[])
    {
        for(int i=0; i<args.length; i++)
           System.out.println("args[" + i + "]: " +args[i]);
    }
}

To execute this program, go to command prompt and write
javac codewindow.java
java codewindow this is a test 100 300
When you do, you will see the following output:

args[0]: this
args[1]: is
args[2]: a
args[3]: test
args[4]: 100
args[5]: 300

Latest Posts

Archives

Leave a Comment

Your email address will not be published. Required fields are marked *