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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this short tutorial, we’re going to see how to run arbitrary main methods from any Java class using Maven.

2. The exec-maven-plugin

Let’s suppose we have the following class:

public class Exec {

    private static final Logger LOGGER = LoggerFactory.getLogger(Exec.class);

    public static void main(String[] args) {
        LOGGER.info("Running the main method");
        if (args.length > 0) {
            LOGGER.info("List of arguments: {}", Arrays.toString(args));
        }
    }
}

And we want to execute its main method from the command line via Maven.

In order to do this, we can use the exec-maven-plugin. To be more specific, the exec:java goal from this plugin executes the supplied Java class with the enclosing project’s dependencies as the classpath.

To execute the main method of the Exec class, we have to pass the fully qualified name of the class to the plugin:

$ mvn compile exec:java -Dexec.mainClass="com.baeldung.main.Exec"
02:26:45.112 INFO com.baeldung.main.Exec - Running the main method

As shown above, we’re using the exec.mainClass system property to pass the fully qualified class name.

Also, we have to make sure that the classpath is ready before running the main method. That’s why we’re compiling the source code before executing the main method.

We can achieve the same thing with plain java and javac. However, this can be cumbersome when we’re working with a pretty large classpath. On the contrary, when using this plugin, Maven automatically takes care of populating the classpath.

3. Passing Arguments

It’s also possible to pass arguments from the command line to the main method. In order to do that, we can use the exec.args system property:

$ mvn compile exec:java -Dexec.mainClass="com.baeldung.main.Exec" \
  -Dexec.args="First Second"
02:31:08.235 INFO com.baeldung.main.Exec - Running the main method
02:31:08.236 INFO com.baeldung.main.Exec - List of arguments: [First, Second]

As shown above, we’re passing a space-separated list of arguments. Moreover, we can use a comma-separated list of arguments via the exec.arguments system property:

$ mvn compile exec:java -Dexec.mainClass="com.baeldung.main.Exec" \ 
  -Dexec.arguments="Hello World,Bye"
02:32:25.616 INFO com.baeldung.main.Exec - Running the main method
02:32:25.618 INFO com.baeldung.main.Exec - List of arguments: [Hello World, Bye]

These two options can be useful when we want to use the delimiter (space or comma) in the argument itself.

4. Custom Configuration

We can also explicitly declare the plugin dependency in our pom.xml. This way, we can use custom and default configurations.

For instance, we can specify a default main class in the plugin’s configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <mainClass>com.baeldung.main.Exec</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Now if we don’t specify the fully qualified name of the desired class, com.baeldung.main.Exec will be used:

$ mvn compile exec:java
02:33:14.197 INFO com.baeldung.main.Exec - Running the main method

However, it’s still possible to override this default configuration via an explicit exec.mainClass system property.

Moreover, we can also specify default program arguments in our configuration:

<configuration>
    <mainClass>com.baeldung.main.Exec</mainClass>
    <arguments>
        <argument>First</argument>
        <argument>Second</argument>
    </arguments>
</configuration>

This way we won’t need to pass these arguments on the command line:

$ mvn clean compile exec:java
02:34:24.448 INFO com.baeldung.main.Exec - Running the main method
02:34:24.450 INFO com.baeldung.main.Exec - List of arguments: [First, Second]

In addition to these configurations, there are plenty more available which are covered in the official documentation.

5. Conclusion

In this short article, we saw how to run the main methods from the command line via exec-maven-plugin.

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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW