Home » News » How to Take Command Line Arguments in Java

How to Take Command Line Arguments in Java

java command line argument

Sometimes you may want to pass information into a program during runtime. This can be done by passing command line argument to main() method. Command line argument is nothing but the information that directly follows the program’s name on the command line when the program is executed. This information is stored as String array passed to main(). This command line argument can easily be accessed within the java program. We will see two examples to get how it works. Since now you have well understood how to create objects Java programs, you can run these programs quite easily.

Program 1

class CommandLineDemo{
    public static void main(String args[]){
        System.out.println("Hello "+args[0]+"!!!");
    }
}

Output

java command line argument

Explanation of the Java Code & Outputs

Please closely observe all the different types of execution of the same java program.

Case 1:

See during first time execution we have passed information “World” as command line argument immediately after the java program name “CommandLineDemo”, which will be passed to main() method as String(in Java String is a predefined class, will discuss later) i.e. this “World” String will be stored in args[] String array at its 0’th position.

Now come to the program, see the print statement, first we will print “Hello ” to the console then very first argument passed through command line will get printed followed by three exclamation symbols. In java using “+” strings are concatenated. Moreover we put Strings within double quotes(” ”).

Case 2:

See during second time of execution of the same program we didn’t pass any information to main() method through command line, it has thrown an exception(Unexpected error during execution of the java program) as args[] String array doesn’t have any String within it, despite we want to print the String at its 0’th position.

Case 3:

See during third time of execution of the same program we have passed more than one argument “My World” to main() method through command line, i.e. two arguments will be stored in the String array args[] at its 0’th and 1’st position of args[], as we want to print the String present at its 0’th position not the String of any other positions we will get the output as shown in the screen “Hello My!!!

Program 2

class CommandLineDemo2{
    public static void main(String args[]){
        int argsLength = args.length;
        if(argsLength >0){
            for(int argsCount = 0;argsCount<=(argsLength-1);argsCount++){
                System.out.println("args["+argsCount+"] :"+args[argsCount]);
            }
        }
        else{
            System.out.println("No arguments passed");
        }
    }
}

Output

java command line argument

Explanation

In the code, first we have measured the length of the String array based on the number of arguments passed to main() method through command line. Now if we don’t pass any argument to main() then the length of the String array args[] will be 0, definitely it will execute the statement in the else part “No arguments passed”.

Now, during second time execution of the same program we have passed some information “Sun rises in the east” .Each of the space separated word will be stored in the String array args[] starting from 0’th up to the position (Number of Words -1)’th position. This String array args[] with 5 Strings is passed to main() method. Therefore the length of the args[] String array will be 5.By iterating from 0 to 4 we have printed all the strings stored in args[] String array from 0’th to 4’th position.

Next we will learn about Java instance variables.

Checkout more useful tutorials and definitive guidelines on Java programming here.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.