Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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 code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW