Command line arguments in Java

We can pass the input arguments to the Java program through command line. main() method accepts those arguments in the form of string array.

Below example explains how we can use command line arguments in Java.

package commandline;

 * Created by ssalunke on 20/04/2016.
public class CommandLine {

    public static void main(String [] args) {
        //javac commandline/
        //java -cp . commandline.CommandLine sagar salunke
        System.out.println("Total number of arguments -> " + args.length);
        System.out.println("value of first argument -> " + args[0]);
        System.out.println("value of second argument -> " + args[1]);

Here is the output of above Java example.

Total number of arguments -> 2
value of first argument -> Sagar
value of second argument -> Salunke

You may also like...