Back to: Java Tutorials For Beginners and Professionals
Command Line Arguments in Java with Examples
In this article, I am going to discuss Command Line Arguments in Java with Examples. 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 Applications.
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 the main( ). A command-line argument is an 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:
Example to Understand How to Pass 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 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 the 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
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.
Example to Pass Numeric Command Line Arguments in Java
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:
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() methods 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:
- They are captured into the String args of your main method.
- Command Line Arguments can be used to specify configuration information while launching your application.
- Information is passed as Strings.
- 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 Command-Line Arguments in Java with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.