Java

Java Command Line Argument Processing

The command line can be used to pass any number of arguments to a Java program. In some programs, the user may need to specify configuration information. While executing such programs which require arguments, the user gives command-line parameters following the name of the class. These command-line parameters might contain various configuration data that your program needs to function properly.

How to perform command line argument processing in Java

Now, in this section, we will guide you on how you can do the command line argument processing in Java. So first, you need to create a public class where we are going to write all the remaining code as shown below:

public class CommandLineArgs

After defining the main function:

public static void main(String[] args)

Next, we have to use a function ‘args.length’ which will be used to calculate the number of arguments passed by the user:

System.out.println("Example 1: Getting number of command line arguments to program");
System.out.println("No of arguments passed to the program: "+ args.length);

Now let’s take another example in which we will calculate the length of an argument which was passed by the user. So, to calculate the length its value should be greater than zero that’s why we have used the if condition and the length of the argument will be calculated by the code ‘args[0].length’.

System.out.println("Example 2: Getting length of the first argument to the program");
if(args.length>=1)
System.out.println("Length of the argument: "+ args[0].length());

In the third example, we are going to add two numbers passed by the user using the ‘try and catch’ process. When running Java code, a variety of mistakes might occur, including programmer errors, problems caused by incorrect input, and other unforeseen events. So, If you put your code inside the try body and any error occurs there during execution then the catch statement will execute that will show you exceptions.

try
{
 double a=Double.parseDouble(args[0]);              
 double b=Double.parseDouble(args[1]);
 double c=a+b;
 System.out.println("Sum of two input arguments is: "+ c);
}

In the above code, we have used a double type argument with parse() function which is the process of extracting the essential information from a piece of data. Now if there is an error occurring while execution due to any reason then the ‘catch’ body will execute while explaining the error as shown below.

catch(Exception ex)
{
System.err.println("Either of the program arguments is not a number");
}

Now the whole code that is used to execute the command line argument process is shown below:

public class CommandLineArgs {
    public static void main(String[] args)
    {
        //Passing command line arguments to main
        //Example 1: Getting length of command-line arguments
        System.out.println("Example 1: Getting number of command line arguments to program");
        System.out.println("No of arguments passed to the program: "+ args.length); /variable
        System.out.println("===========");
       
        //Example 2: Getting length of command-line arguments
        System.out.println("Example 2: Getting length of the first argument to the program");
        if(args.length>=1)
            System.out.println("Length of the first argument: "+ args[0].length()); /function
        System.out.println("===========");
       
        //Example 3: Performing addition on two input arguments
        System.out.println("Example 3: Performing addition of the two program arguments");
        if(args.length>=2)
        {
            try
            {
                double a=Double.parseDouble(args[0]);/parse (translate or convert)
                double b=Double.parseDouble(args[1]);
                double c=a+b;
                System.out.println("Sum of two input arguments is: "+ c);
            }
            catch(Exception ex)
            {
                System.err.println("Either of the program arguments is not a number");
            }
        }
        System.out.println("===========");
    }
}

If you want to execute this program in the Linux operating system then you can easily do that using any text editor. For that, you can open a terminal and type:

$ nano CommandLineArgs.java

After that, you need to write your code in the file editor and then save it:

Now you need to first compile the code before executing it and you can do that by typing:

$ javac CommandLineArgs

Now after compiling the code you need to pass some arguments before executing the code for example:

$ java CommandLineArgs 5 10

$ java CommandLineArgs 1234 56

Conclusion

The command line can be used to give any number of arguments to a Java program, and the user can input configuration information when the program is executed. A Java command-line application gets arguments from the user first and then executes the code based on those arguments. These command-line options include various configuration data that your software needs for optimal operation.

About the author

Taimoor Mohsin

Page was generated in 0.062661170959473