Run Java Classes with Maven Exec | Generated by AI

Home 2025.08

The exec:java goal of the Maven Exec Plugin is a powerful way to run a Java class with a main method directly from your Maven build. It executes the specified Java class within the same JVM as your Maven build, using the project’s dependencies as the classpath.

Here’s how to use it, with common scenarios and examples:

1. Basic Usage from the Command Line

The simplest way to use exec:java is directly from the command line, especially for quick tests or running a specific utility class.

Command:

mvn exec:java -Dexec.mainClass="com.yourcompany.YourMainClass"

Explanation:

Example:

Let’s say you have a class com.example.myapp.HelloWorld with a main method:

package com.example.myapp;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from Maven exec:java!");
    }
}

You would run it using:

mvn compile exec:java -Dexec.mainClass="com.example.myapp.HelloWorld"

Note: It’s good practice to run mvn compile first to ensure your classes are compiled before exec:java attempts to run them.

2. Passing Arguments to Your Java Program

You can pass arguments to your Java program’s main method using the exec.args system property:

Command:

mvn exec:java -Dexec.mainClass="com.yourcompany.YourMainClass" -Dexec.args="arg1 arg2 \"arg with spaces\""

Example:

If your HelloWorld class was:

package com.example.myapp;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from Maven exec:java!");
        if (args.length > 0) {
            System.out.println("Arguments received: ");
            for (String arg : args) {
                System.out.println("- " + arg);
            }
        }
    }
}

You would run it like:

mvn compile exec:java -Dexec.mainClass="com.example.myapp.HelloWorld" -Dexec.args="FirstArgument SecondArgument"

For arguments with spaces, enclose them in quotes:

mvn compile exec:java -Dexec.mainClass="com.example.myapp.HelloWorld" -Dexec.args="\"Hello World\" AnotherArg"

3. Configuring exec:java in pom.xml

For more permanent or default configurations, you can add the exec-maven-plugin to your pom.xml. This allows you to define a default mainClass and other parameters, so you don’t have to specify them on the command line every time.

pom.xml configuration:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yourcompany</groupId>
    <artifactId>my-maven-app</artifactId>
    <version>1.0-SNAPSHOT</modelVersion>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.2.0</version> <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.example.myapp.HelloWorld</mainClass>
                    <arguments>
                        <argument>defaultArg1</argument>
                        <argument>defaultArg2</argument>
                    </arguments>
                    <systemProperties>
                        <systemProperty>
                            <key>my.custom.property</key>
                            <value>someValue</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>

Explanation of configuration options:

Running with pom.xml configuration:

Once configured in pom.xml:

4. Key Differences from exec:exec

It’s important to understand the distinction between exec:java and exec:exec:

For most standard Java application execution within a Maven build, exec:java is the more convenient choice.

5. Important Considerations


Back Donate