Course – LS (cat=Java)

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

Command-line arguments are a powerful and useful tool for providing additional information and instructions to a command-line program at runtime.

In Java, they can be accessed through the args array of String objects, which is automatically created by the Java runtime when the program is called with command-line arguments. However, it's important to check if command-line arguments are null in order to properly handle cases where they are not provided or where they are invalid or unexpected.

In this tutorial, we'll discuss how to check if command-line arguments are missing.

2. Accessing the Command-Line Arguments

To access and use command-line arguments in a program, we can simply reference the elements of the args array:

public class CommandLineWithoutErrorHandling {

    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

This program simply prints the first command-line argument to the console:

java CommandLineWithoutErrorHandling.java arg1 arg2 arg3

The output of this command-line is arg1.

Additionally, we can access and use other command-line arguments in a similar manner. For instance, to access the second command-line argument, we can use args[1], and so on.

However, if the args array is empty, then attempting to access its elements will result in an ArrayIndexOutOfBoundsException:

@Test(expected = NullPointerException.class)
public void givenNullCommandLineArgument_whenPassedToMainFunction_thenExpectNullPointerException() {

    CommandLineWithoutErrorHandling.main(null);
}

It's important to note that we should always check the length of the args array to ensure that it's non-empty before attempting to access its elements:

public static void main(String[] args) {
        
    if (args.length > 0) {
        System.out.println(args[0]);
    } else {
        System.out.println("No command line arguments were provided.");
    }
}

Consequently, this program will output the first command-line argument if it's provided or a message stating that no command-line arguments were provided if the args array is empty.

3. Check if Command-Line Arguments Are Missing

To check if command-line arguments are missing, we can use one of the following approaches.

Firstly, we can check if the args array is null:

if (args == null) {
    // No command line arguments were provided
} else {
    // Command line arguments were provided
}

Secondly, we can check the length of the args array to determine if any command-line arguments were provided. If the length is zero, it means no arguments were provided:

if (args.length == 0) {
    // No command line arguments were provided
} else {
    // Command line arguments were provided
}

Finally, we can check if any command-line arguments were provided, regardless of whether they are null or not:

if (args.length > 0) {
    // Command line arguments were provided
} else {
    // No command line arguments were provided
}

Each of these approaches allows us to determine whether or not command-line arguments were provided to our program.

4. Conclusion

In this article, we looked at different methods for checking if the command-line arguments are missing in a Java program.

We discussed the benefits and considerations of each approach and emphasized the importance of checking for null arguments in order to handle cases where required arguments are not provided, or invalid ones are received. This is crucial for determining the correct behavior of the program and ensuring that it runs smoothly.

The complete source code for this tutorial is available over on GitHub.

Course – LS (cat=Java)

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
guest
0 Comments
Inline Feedbacks
View all comments