Register Login

Command line Argument in Java

01 Apr 2021 1:03 am || 0

Java has all the elements and operations that other programming languages also have. Among them, the command-line argument is one of the most powerful features that Java provides to its programmers. In this chapter, you will learn about what are command-line arguments, their syntax, and how to use them within a Java program.

What is Command-line Argument in Java?

The command-line argument is a unique Java feature that allows programmers to design the program in such a way that users can pass some information at the time of program execution. The information given at the time of program execution remains stored in the string array of the main() method. This stored information then gets directly used in the program at the time of the program run.

The flexibility is you can pass your information as an argument to the main() from within the console. The Java program will catch those arguments as input for the program and use them within the program.

Syntax

public static void main(String variableName[])

When you run the class Tested and want to specify your command line arguments, you have to write it like this:

java Tested argument1 argument2 argument3 ….

Java will then supply the command-line arguments to JVM. JVM will then wrap the information passed to the main and provide it to args[].

Program Example

Here is a simple program showing the working of the Command-line argument –

class Main {  
public static void main(String args[])
    {  
    System.out.println(" The argument received from main is : " + args[0]);  
    }  
}

Explanation:

In this program within the Main class, we have called the main() with a String array variable args[] that will take an argument as information at the time of program run. System.out.println() will then display that argument from within the main().

How to Pass Command-line Argument in Java?

To pass command-line arguments in Java, you have to execute the program first. At the time of execution, you have to type the command

java <program-Name> pass_Argument1, pass_Argument2, pass_Argument3, ...

You have to mention the program name followed by the set of arguments separated by commas. Each argument will take one array location from the args[]. Programmers can access using the array index within the subscript. Let us now take a look at one program where we will fetch each value separately from the args[] using a loop.

Code Example

class Main {  
public static void main(String args[])
    {  
  
    for(int k=0; k < args.length; k++)  {
        System.out.println(args[k] + " \n ");  
        }
    }  
}  

In this program, we have used the command-line argument passing String array args[]. Now, inside the main(), we have created a loop where we will fetch each value of args[] array using the index value that will iterate from 0 to k-1. Putting the value of 'k' within the args[] subscript as index value will allow us to get each array value. The System.out.println() will display all the fetched values one by one.

Uses of Command-line Argument in Java

  • Programmers use command-line arguments to provide data at program execution.
  • Passing values in the command-line argument makes program execution faster.
  • Without hard-coding the program inside the main(), command-line arguments let you share the information from outside the program.
  • If your software does not want any GUI interface for entering values and wants a CLI interface only, a command-line is the best alternative as it uses the console.
  • Programmers do not have to alter any part of the program or the program as a whole to change the command-line argument values.

Conclusion

It is essential to know how to use command-line argument because it helps in delivering program input at the time of execution, making the program less complex. The command-line argument in Java takes a string array for multiple information fetched during runtime.