The command-line argument is a concept where users can pass some information as a string at the time of program execution. The information given at the time of 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 moment you pass the argument to the main() from within the console, Java 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 and want to specify your command line arguments, you have to write it like this:
$java class-name 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[].
Example:
public class Main{
public static void main(String args[])
{
System.out.println(" The argument received from the 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().
Compile the code:
$javac Main.java
Execute:
$java Main Hey
Output:
The argument received from the main is Hey
How to Pass Command-line Argument?
To pass command-line arguments in Java, you have to compile the program first.
javac programName.java
Now, at the time of execution, you have to type the syntax –
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.
Example:
public class Main{
public static void main(String args[]) {
int k=0;
while(k < args.length) {
System.out.println(args[k]+" \n ");
k++;
}
}
}
java comdlinearg Hi this is Stechies
Output:
Hi
this
is
Stechies
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.
How to Pass Arguments using IDE?
We can pass command-line arguments in a separate panel from where we can provide the information. The steps to pass arguments in the IDE are:
- Open the Eclipse IDE.
- Right-click the code section and choose "Run As" > "Run Configurations" option.
- The Run Configuration window will appear.
- From the Arguments tab, under the Program Arguments, provide your arguments on that text box and hit the Run button.
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.