Taking Command Line Arguments in Java

Advertisement

Advertisement

Overview

Introduction

Taking command line arguments is one of the first things you should learn how to do with a new language. In this tutorial we'll walk through a simple Java program that takes command line arguments. We'll look at how to check if any arguments were passed, how many arguments were passed, access them directly by numerical index, and iterate through each argument provided. For this example, JDK 1.8 was used.

When you create a class in Java with a main() function, the main function must match the proper method signature, that is:

public static void main(String[] args) {}

Print all arguments passed

The command line arguments are stored in the array of String objects passed to the main function. You could print out the arguments by simply creating a class like this:

// PrintCommandLineArgs.java
class PrintCommandLineArgs {
    public static void main(String[] args) {
        // Iterate through each string in the args array
        for (String arg: args) {
            System.out.println(arg);
        }
    }
}

And then compiling and running it like this:

javac PrintCommandLineArgs.java
java PrintCommandLineArgs arg1 arg2 3 4 5

Check how many arguments (if any) were provided

That example will print out one argument per line. One difference between Java and other languages, is that Java does not always pass argument 0 with the name of the program being executed itself. In other languages, you are guaranteed to have at least one argument, being the program itself. In Java, it is possible to get an empty (length 0) array if no arguments are provided. You can check to see if any arguments were passed, and then access the arguments by their numerical index, like this:

// CheckForCommandLineArgs.java
class CheckForCommandLineArgs {
    public static void main(String[] args) {
        if (args.length > 0) { // If any arguments provided
            System.out.println(args[0]); // Print only the first argument
        } else {
            System.out.println("No arguments provided.");
        }
    }
}

Then compile and run it like this:

javac CheckForCommandLineArgs.java
java CheckForCommandLineArgs
java CheckForCommandLineArgs myArgument

You will notice the first time you run it, it tells you "No arguments provided." and on the second run it printed out the first argument provided.

Check if argument equals a value

One last example will demonstrate how to check for a specific value passed as an argument. For example, if the "--help" flag is provided, we want to print out a message and exit the program.

// CheckForHelpFlag.java
class CheckForHelpFlag {
    public static void main(String[] args) {
        for (String arg: args) {
            if (arg.equals("--help") || arg.equals("-h")) {
                System.out.println("Help argument (--help) detected.");
                System.exit(0);
            }
        }
    }
}

And then compiling and running it like this:

javac CheckForHelpFlag.java
java CheckForHelpFlag
java CheckForHelpFlag --help
java CheckForHelpFlag -h

Conclusion

With this knowledge, you should be able to check if any command line arguments were provided, determine the length (count) of arguments provided, directly access a specific argument, check if an argument equals a value, and iterate through each argument provided.

Advertisement

Advertisement