How to Implement Command Line Arguments in Java

Last updated on Jun 17,2021 8.4K Views

How to Implement Command Line Arguments in Java

edureka.co

The command-line argument in Java are the arguments passed to a program at the time when you run it. They are stored in the string format and the String array is passed to the args[] parameter of main() method. We shall learn more through this docket below.

 

First Java Program

You want to develop a program in Java to print “Hello World” on screen. What steps will you consider?

 

class MyFirstJavaProgram { 
       public static void main(String[] args) {
              System.out.println(“Hello World”);
       }
}

 

 

Command Line Arguments

The syntax of java command to launch an application is 

Java [options] main class [args]

Here, options are the command-line options such as -ea, etc. main class is the name of the class containing the main method. Specifically, the class that you want to launch.

Lastly [args] are the arguments that are passed to the main method. In our program, we have specified String array as a parameter. Hence multiple arguments can be passed using spaces.

By default, Java application can accept any number of arguments including no arguments from the command line. This can be used to specify configuration information when the application is launched. That means suppose we do not know the details of configuration before running the application.

That details can be reused in your application such that it does not need to be hard-coded. However, the configuration information can be given in a character string or in a file. Will that hamper my application Performance?

The answer is, No. This will not hamper in any case the application performance overall. Because we are only reading the configuration file at once only during the application startup. In this way, you do not have to recompile your code again and again.

Let’s say, for example, we have a config.properties file which is having all the configuration properties in key-value format. But as a junior developer, we don’t even know where this file is kept on client location after we ship the application.

What we can say that the client will have to specify the path as an argument to the application over the command line.

java MyClass “c://path/to/config.properties”

When MyClass is launched the runtime system passes the command line arguments to the application main method via an array of String here it was a full path. 

Consider, a simple program to demonstrate the entries made by the user from the command line will be printed itself.

public class MyClass {
      public static void main(String[] args){
            for(String s : args) {
                  System.out.println(s);
            }
      }
}

To run this program user might enter input like below.

java MyClassI have created my first program.” “This is awesome.”

//Output:

I have created my first program.
This is awesome.

 

This is to note that each String enclosed in quotation mark and separated by space is considered a new argument. Every argument is placed on new line cause of println.

What if I want Number to pass as command-line arguments?

To be able to read numbers we must parse Numeric Command-Line Arguments. This is because java only takes an array of Strings as an argument to its main method. Therefore, we must convert String argument that represents a number such as “10” to its numeric value.

Let’s take an example by constructing a program.

class SquareCalculator{
     public static void main(String[] args){
          int firstArg;
          if(args.length > 0){
                try{
                       firstArg = Integer.parseInt(args[0]);
                } 
                catch(NumberFormatException nfe) {
                       System.out.println(“Arguments” + args[0] + “ must be an integer.”);
                       System.exit(1);
                }
                System.out.println(“Square  is ” + firstArg * firstArg);
          }
     }
}

To run this application we use below line

java SquareCalculator 10

//Output:

Square is 100

 

Command Line Arguments in an IDE

The beauty relies on the parse Int method in Integer class. Every Number classes such as Integer, Float, Double and so on have parseXXX method that converts String into the respective object of their type.

As we all know that array starts its index with zero. Therefore args[0] is the first index in this String[] array which is taken from the console. Similarly, args[1] is second, args[2] is the third element and so on.

When an application is launched, the run-time system passes the command-line arguments to the application’s main method via an array of Strings. 

How do we pass the Command Line Argument using eclipse IDE?

//Output:

Hello World

 

Here things to keep in mind is the white-space is to separate two arguments with each other and not for representation purpose.

This was basic usage of command-line arguments for more advanced usage of CLA we can use it in Diagnostic management for the Hot-Spot Virtual Machine. Virtual Machine can also be provided with Command Line Arguments using Virtual Machine Option for Hot-Spot Diagnostic that may be used when you want to invoke any bean from Server Connection. That is food for thought!

With this, we come to an end of this “Command Line arguments in Java” article. I hope you have understood the Command Line arguments in Java and its implementation through some real-time examples.

Now that you have understood Command Line arguments basics through this article check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification courses are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Mention it in the comments section of this “Command Line arguments in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES