Command-Line Arguments

Command-Line Arguments in Java

When you run a Java program, you can pass input to it from the command line. This is where command-line arguments come in—they let you provide data to your program as it starts.

In this post, you'll learn:

  • What command-line arguments are

  • How to use them in Java

  • A real example

  • Tips and common mistakes

What Are Command-Line Arguments?

Command-line arguments are values passed to the main() method when the program is executed.

In Java, the main() method is defined as:

public static void main(String[] args)

Here, args is an array of strings (String[]), and each element of this array is one argument passed from the command line.

The command-line arguments are stored as array of strings which is passed to main( ) method. You have to use the index for accessing the argument which starts from 0 as given below program.

  • Here we pass two numbers using command line and add these two numbers into sum variable and display it. 
  • The Integer.parseInt(args[0]) uses parseInt( ) method of Integer wrapper class which converts string object to primitive integer type. Note that when you pass command line arguments they are collected in a string array.
  • The program source code is given below:

Example: A Simple Program That Uses Arguments

Let’s create a program that prints out all the arguments passed to it:

public class CommandLineExample {

    public static void main(String[] args) {

        System.out.println("You passed " + args.length + " arguments:");

        for (int i = 0; i < args.length; i++) {

            System.out.println("Argument " + (i + 1) + ": " + args[i]);

        }

    }

}

Save the file: Save the above program in file name as CommandLineExample.java

How to Run It:

Follow these steps:

1. Compile the Program

    javac CommandLineExample.java

2. Run with Arguments

    java CommandLineExample Java is fun


Output:

You passed 3 arguments:

Argument 1: Java

Argument 2: is

Argument 3: fun

Each space-separated word is treated as a separate argument.


Tips:

Command line arguments are strings by default. If you want to use numbers, you’ll need to convert them:

    int number = Integer.parseInt(args[0]);

Enclose multi-word arguments in quotes:

    java CommandLineExample "Java is cool"


Example: Program to accept two integer numbers using command-line arguments and display the sum of those numbers.

class CmdLineArgDemo {

 public static void main(String[] args) {

  int no1 = Integer.parseInt(args[0]);
  int no2 = Integer.parseInt(args[1]);
  
  int sum = no1 + no2;
  System.out.println("Addition of two numbers is "+sum);
 }

}


The output of the code is given below:

OUTPUT

C:\>javac CmdLineArgDemo.java
C:\>java CmdLineArgDemo 10 21
Addition of two numbers is 31


In this simple program the numbers 10 and 21 are passed as arguments to java interpreter while running the program.


Final Thoughts

Command-line arguments are a handy way to provide input to Java programs without changing code. They're great for scripting, automation, and building flexible utilities.

Now that you know how to work with them, try building a small calculator or text analyzer that takes arguments from the command line. Practice is the best teacher!

Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details