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:
After defining the main function:
Next, we have to use a function ‘args.length’ which will be used to calculate the number of arguments passed by the user:
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.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.
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.
Now the whole code that is used to execute the command line argument process is shown below:
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
//Example 2: Getting length of command-line arguments
System.out.println("Example 2: Getting length of the first argument to the program");
System.out.println("Length of the first argument: "+ args.length()); /function
//Example 3: Performing addition on two input arguments
System.out.println("Example 3: Performing addition of the two program arguments");
double a=Double.parseDouble(args);/parse (translate or convert)
System.out.println("Sum of two input arguments is: "+ c);
System.err.println("Either of the program arguments is not a number");
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:
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:
Now after compiling the code you need to pass some arguments before executing the code for example:
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.