Command Line Arguments in Java

23 Jun 2025 | 4 min read

The Java command-line argument is a way to pass arguments in a program at runtime. The arguments passed from the console can be received in the Java program, and those arguments can be used as input.

The inputs are supplied as a String array[] in the main() method to let programmers use different values without making changes to the code.

So, it provides a convenient way to check the behavior of the program for different values. We can pass N (1, 2, 3, and so on) numbers of arguments from the command prompt.

Why Use Command-Line Arguments?

Command-line arguments offer a flexible way to pass input values to Java programs without changing the source code. It is particularly useful in scenarios like automation, where scripts need to run with different inputs, or configuration management, where settings change between environments (for example, dev, test, prod).

They allow developers to test various input scenarios efficiently, making programs more dynamic and reusable. Rather than hardcoding values, users can supply them externally, which leads to cleaner code and easier maintenance.

Example of Command Line Argument

In this example, we are receiving only one argument and printing it. To run this Java program, we must pass at least one argument from the command prompt.

Output:

Your first argument is: Andrew

Execute the above program by using the following commands.

How to Handle Missing Arguments?

Accessing arguments that were not passed will cause an ArrayIndexOutOfBoundsException. To avoid this, always check the length of the args[] array:

The above code snippet avoids crashes and improves the reliability of your code.

Converting Argument Types

All command-line arguments are passed as strings. If your logic needs integers, floats, or booleans, we must parse them:

Always handle exceptions like NumberFormatException when converting input values.

Example: Printing Multiple Values Using Command Line Arguments

In this example, we are printing all the arguments passed from the command line. For this purpose, we have traversed the array using a for loop.

Output:

Andrew
25
University 
of
California 

Execute the above program by using the following commands.

Important Points to Remember

  1. While running a Java program in the command prompt, arguments are provided in the console.
  2. Arguments are added to the String[] args array used by the main() method.
  3. The first argument is in args[0], the second in args[1] and, so on.
  4. Trying to use a non-existent index will cause an ArrayIndexOutOfBoundsException to be thrown.
  5. The number of arguments sent can be checked with args.length.
  6. We can take advantage of loops (for example, a for loop) to loop through and print out all the arguments.
  7. Every command-line argument, regardless of being a number, is still regarded as a string.

Conclusion

Command line arguments in Java provide a flexible and efficient means of passing input to a programme at runtime. By supplying arguments through the main(String[] args) method, developers can customise programme behaviour without altering the source code. It makes Java applications more dynamic, testable, and suitable for automation.

Java Command Line Arguments MCQs

1. Which method receives command-line arguments in Java?

  1. run()
  2. main()
  3. start()
  4. init()

Answer: b)

Explanation: These arguments are received in the main method as a String[] args array.


2. What is the data type of command-line arguments in Java?

  1. String
  2. Array
  3. String[]
  4. char[]

Answer: c)

Explanation: The inputs are supplied as a String array in the main() method to let programmers use different values without making changes to the code.


3. How many arguments can be passed to the main() method?

  1. 1
  2. 2
  3. 3
  4. Infinite

Answer: d)

Explanation: We can pass N (1, 2, 3, and so on) numbers of arguments from the command prompt.


4. Can command-line arguments be automatically converted to integers?

  1. Yes
  2. No
  3. Only in Java 11+
  4. Depends on JVM

Answer: b)

Explanation: All command-line arguments are passed as strings. If logic requires integers, floats, or booleans, we must parse them.


5. We can input command-line arguments at _________.

  1. Run Time
  2. Compile Time
  3. Both a and b
  4. None of the above

Answer: a)

Explanation: Command line arguments in Java provide a flexible and efficient means of passing input to a program at runtime.


Next TopicObject vs Class