Command Line Arguments in Java

Command Line Arguments in Java

In this article, I am going to discuss Command Line Arguments in Java. Please read our previous article, where we discussed Pass By Value and Pass By Reference in Java with examples. At the end of this article, you will understand how to pass command-line arguments in java application. 

How to pass command-line arguments in java?

Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main( ). A command-line argument is information that directly follows the program’s name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy— they are stored as strings in a String array passed to the args parameter of main( ). The first command-line argument is stored at args[0], the second at args[1], and so on. For example, the following program displays all of the command-line arguments that it is called with:

Sample Program for using Command Line Arguments in Java:
package Demo;
//Display all command-line arguments
public class CommandLineDemo {
 public static void main(String args[]) { 
  for(int i=0; i<args.length; i++) 
   System.out.println("args[" + i + "]: " + args[i]); 

Output :

How to pass command line arguments in java?

How to run your application with Command Line Arguments using Eclipse?

Step-1 : Click on Run -> Run Configurations

Step-2: Click on Argument tab

Step-3: In the Program Arguments section, Enter your arguments you want to run

Step-4: Click Apply

Step-5: And, then click Run

Note: We can pass any number of arguments because the argument type is an array.

Passing Numeric Command Line Arguments

In Java, Arguments are always stored as strings and always separated by white-spaces. The main() method of every Java program only accepts string arguments. If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as “34”, to a numeric value. Hence it is possible to pass numeric arguments through the command line. However, we can later convert string arguments into numeric values.

package Demo;
public class NumericCommandLine {
      public static void main(String args[]) {
     for(String str:args) {
  //convert into integer type
  int argument = Integer.parseInt(str);
  System.out.println("Argument in Integer Form : " +argument);

Output :

Command Line Arguments in Java

In the above example, notice the line int argument = Integer.parseInt(str); Here, parseInt() method of the Integer class converts the string argument into an integer. Similarly, we can use the parseDouble() and parseFloat() method to convert the string into double and float respectively.

Note: If the arguments cannot be converted into the specified numeric value then an exception named “NumberFormatException” occurs.

Important Points:
  1. They are captured into the String args of your main method.
  2. Command Line Arguments can be used to specify configuration information while launching your application.
  3. Information is passed as Strings.
  4. There is no restriction on the number of java command line arguments.

From the next article onwards I am going to start the OOPs Concept in Java. Here, in this article, I try to explain How to pass command line arguments in java with examples and I hope you like this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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