Java Command Line Arguments with Examples

We know that Java is a versatile programming language. It is a platform-independent programming language. The execution of a Java program is less complex, quite easy, and error-free. There are many situations that we face during programming where we need to pass the input while executing the program.

In our previous article, we studied how to take input from the console, in this article, we will learn how a user can pass java command-line arguments during the execution of a program.

What Are Java Command Line Arguments?

The command-line arguments in Java allow the programmers to pass the arguments during the execution of a program. The users can pass the arguments during the execution by passing the command-line arguments inside the main() method.

A command-line argument is nothing but the information that we pass after typing the name of the Java program during the program execution. These arguments get stored as Strings in a String array that is passed to the main() function.

We can use these command-line arguments as input in our Java program.

Java Command-line arguments Example with its working

Suppose there is a java program and we compile it. During the execution of the program, we pass the command as “java MyProgram Techvidvan Java Tutorial”. Here the java command is used to run the Java program in which the main function is present and,

MyProgram is the name of the java file that we need to run. JVM considers the input after the program name as command-line arguments. That is, in our input command, “Techvidvan Java Tutorial” is considered as command-line arguments in the form of a String array.

We need to pass the arguments as space-separated values. We can pass both strings and primitive data types(int, double, float, char, etc) as command-line arguments. These arguments convert into a string array and provided to the main() function as a string array argument.

Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass into the main() function. We can check these arguments using args.length method.

JVM stores the first command-line argument at args[0], the second at args[1], third at args[2], and so on.

Let’s learn how to use command-line arguments in our Java program.

Java code to understand the command-line arguments

To understand the command-line argument we will create a file named CommandLine.java and write the following code to display all the arguments that we will pass through the command-line:

//Display all command-line arguments.
class CommandLine
{
    public static void main(String args[ ])
    {
        System.out.println(“The command-line arguments are:\n”);
        for (int x = 0; x < args.length; x++)
            System.out.println("args[" + x + "]: " + args[ x ]);
    }
}

Steps to run the above program

To compile and run a java program in command prompt, follow the steps written below.

  • Save the program as CommandLine.java
  • Open the command prompt window and compile the program- javac CommandLine.java
  • After a successful compilation of the program, run the following command by writing the arguments- java CommandLine argument-list
  • For example – java CommandLine TechVidvan Java Tutorial
  • Press Enter and you will get the desired output.

After performing the above steps, we will get the following output:

Output:

Command line in java example

Example of command-line argument in java that prints only one argument

We can also display a single argument in the command prompt. Following code shows this example:

//Display one command-line argument.
class CommandLine
{
    public static void main(String args[ ])
    {
        String name = args[3];
        System.out.println("The name of the user is: " +name);
    }
}

Output:

Command line argument in java with example

How to Pass Numeric Command-Line Arguments in Java?

The main() method of a Java program only accepts the data with the string type arguments. Therefore, we can never pass numeric arguments through the command line.

If we pass them, they are converted int string and we can’t use them as numbers and can’t perform any numeric operations with them.

But, we can convert these string(numeric) arguments into numeric values. Let’s see an example to understand it.

public class NumericArguments
{
    public static void main(String[] args)
    {
        // convert into integer type
        int number1 = Integer.parseInt(args[0]);
        System.out.println("First Number: " +number1);

        int number2 = Integer.parseInt(args[1]);
        System.out.println("Second Number: " +number2);
        int result = number1 + number2;
        System.out.println("Addition of two numbers is: " +result);
    }
}

Output:

Passing Numeric Command-Line Arguments in java

In the above program, we used the parseInt() method of the Integer class to convert the string argument into an integer.

Similarly, we can use the parseDouble() to convert the string into a double value and parseFloat() method to convert the string into a float value.

How to Pass Command Line Arguments in Eclipse IDE

We can also pass command-line arguments in Eclipse IDE using Run Configurations.

Step 1

Open the Java program in Eclipse

Open java program in eclipse

Step 2

From the editor, right-click and choose “Run As” option. Inside it, select the “Run Configurations… option”. After choosing Run Configurations, you will see following screen:

Run configuration in java

Step 3

In the pop-up window, click on the Arguments tab. Then provide the command line argument value in the “Program Arguments” text box.

Java arguments tab

Step 4

Click on the Run button

When you will click on the Run button, you will get the following output:

Command line arguments output

Calculating Factorial of a number through the command-line

Now, after having the knowledge about the command-line arguments and their usage, we can start using it in various calculations. Let’s try to calculate the factorial of a number in which we pass the number(whose factorial we need to calculate) as the command-line argument:

public class Factorial
{
    public static void main(String[] args)
    {
        int i , fact = 1;
        int number = Integer.parseInt(args[0]);
        for(i = 1; i<= number ; i++)
        {
            fact = fact * i;
        }
        System.out.println("The factorial of " + number + " is " +fact);
    }
}

Output:

Java factorial example

Important Points To Remember About Command-line Arguments

  • We can use the command-line arguments to specify the configuration information while launching our Java application.
  • We can use as many arguments in the command line as we need to. There is no limitation on the number of command-line arguments.
  • The data in command-line arguments are in the form of String.

Summary

This brings us to the end of the article on Java Command line arguments. We learned that Command line arguments in Java are passed at the execution of the program and the JVM provides them to the args string in the main() method of the Java program.

We came to know how can we pass the String as well as the numeric arguments to the command line as the user input. You can also run the command line arguments in the IDE such as Eclipse whose step by step process id discussed in this article.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.