Skip to content

Java Command-line arguments

Imagine you're preparing a recipe. You gather all your ingredients and spices before you start cooking. In Java, command-line arguments are like those ingredients. They're bits of information you can pass to your Java program when you run it from the command line.

Now, let's make it even simpler. Say you have a program that greets the user by name. Instead of hardcoding the name into your program, you can use command-line arguments to let the user input their name when they run the program.

Here's a quick breakdown of how it works:

  1. Writing Your Program: First, you write your Java program as usual, but with a twist. You make it so that it can accept input from the command line.

  2. Passing Arguments: When you run your program from the command line, you can add extra information after the program's name. These are your command-line arguments.

  3. Accessing Arguments: Inside your Java program, you can access these arguments and use them however you need. It's like reaching into your bag of ingredients while cooking!

  4. Running Your Program: Finally, you run your program with the command-line arguments, and voila! Your program reacts based on the input it receives.

Let's put it into practice with a simple example:

java
public class Greeting {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("Hello, " + args[0] + "!");
        } else {
            System.out.println("Hello, World!");
        }
    }
}

In this example, if you run the program like this: java Greeting Alice, it will output: Hello, Alice!. But if you run it without any arguments, it will output the default greeting: Hello, World!.

java
public class CommandLineArgumentsExample {
    public static void main(String[] args) {
        // Check if any arguments were provided
        if (args.length == 0) {
            System.out.println("No arguments provided!");
        } else {
            // Print each argument provided
            System.out.println("Arguments provided:");
            for (int i = 0; i < args.length; i++) {
                System.out.println((i + 1) + ". " + args[i]);
            }
        }
    }
}

In this example, we have a simple Java program that prints out any command-line arguments passed to it. Here's how you can run it from the command line:

java CommandLineArgumentsExample arg1 arg2 arg3

When you run the program with these arguments, it will output:

Arguments provided:
1. arg1
2. arg2
3. arg3

Pretty neat, huh? You can pass as many arguments as you like, and your program will handle them gracefully.

But wait, there's more! Command-line arguments aren't just limited to strings—you can pass numbers, file paths, or any other type of data you need. Plus, you can use them to control the behavior of your program, like specifying configuration settings or input parameters.

Let's explore another example where we calculate the sum of numbers passed as command-line arguments:

java
public class SumCalculator {
    public static void main(String[] args) {
        double sum = 0.0;
        for (String arg : args) {
            sum += Double.parseDouble(arg);
        }
        System.out.println("Sum of numbers: " + sum);
    }
}

Now, you can run this program with numbers as arguments, like so:

java SumCalculator 10 20 30.5 40.2

And it will output:

Sum of numbers: 100.7

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.