Command Line Arguments in Java

Basically, there are 2 methods by which one can take the inputs from the user via command line arguments i.e.

  1. Command Line Arguments
  2. Scanner Class

Command Line Arguments are the arguments that are given while running the program through command prompt console. One can pass the CLA through command prompt in the following way,

java classname values

Note-

Command line arguments should be separated by a space between them. If space is omitted then all the values will be considered as one value. . For example

java Student Bill

Generally, all the arguments are received as strings. Consider the following code which takes up arguments and prints them on screen.

class Student
{
public static void main(String []args)
{
System.out.println("Arguments passed are:");
for(int i=0;i<args.length;i++)                  //looping the array
{
System.out.println(args[i]);                    //printing values of array
}
}
}

Giving inputs to this code while execution using console:

javac Student.java
java Student Bill Robert Mike

The program received all the above arguments in an array of String. Its output will be:

Arguments passed are:
Bill 
Robert 
Mike

With the command line arguments:

  • Limitation for number of arguments is 2147483647.
  • All the arguments will only be given in form of String. Even if integer or double type data it is given, it will be converted to String type. For example:
class Student
{
public static void main(String []args)
{
System.out.println("Adding the arguments: ");
System.out.println(args[0]+args[1]+args[2]);      //adding three arguments
}
}

Input is given as:

java Student 1 56 43

Output:

Adding the arguments: 
15643

Note-  As you can see instead of adding up the arguments they are concatenated. So to convert String type into other datatype parsing is needed to be done. To add up numbers rather than concatenating them following code will be used.

class Student
{
public static void main(String []args)
{
System.out.println("Adding the arguments: ");
System.out.println(Integer.parseInt(args[0])+Integer.parseInt(args[1])+Integer.parseInt(args[2]));      
//adding three arguments
}
}

Input is given as:

java Student 1 56 43

Output:

Adding the arguments: 
100

Note-

But if in this code, arguments are passed other than int datatype which is not compatible with int, then the compiler will throw an exception as NumberFormatException.

If arguments already contain a space between them just like spaces between sentence then we should enclose such arguments in double quotes (” “). For example:

java Student "Mark Ruffalo"

Advantages of CLA

  1. Certain programs can be used to launch with CLA by passing the parameters as the arguments.
  2. Database files can be exported and imported using the java program rather than doing it manually through command prompt.

This is all about the command line arguments in Java.

Leave a Comment

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