Skip to main content

How to Run a Java Program with Maven (exec:java)

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Maven compiles Java code, but it doesn't run it on its own. The Exec Maven Plugin adds that: name the class to run in pom.xml, and mvn compile exec:java compiles the project and runs it in one command, with every library the project depends on already on the classpath.

Syntax​

In pom.xml, inside <build><plugins>:

Syntax: pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass><package>.<Class></mainClass>
</configuration>
</plugin>

Then:

Syntax
mvn compile exec:java ["-Dexec.args=<arguments>"]
PartWhat it does
<mainClass>The class whose main method runs, with its package, such as com.example.App.
compileCompiles the project first. exec:java only runs what's already compiled.
exec:javaRuns the class inside Maven's own Java process, with the project's dependencies on the classpath.
"-Dexec.args=<arguments>"Optional. The arguments the program receives in args. Keep the quotes around the whole thing: PowerShell needs them.

The Example​

The example is a project named hello-exec whose App class greets the name it receives, or the whole world when it receives none.

Prerequisites​

Step 1: Write the Project​

Terminal
mkdir hello-exec
cd hello-exec

Create pom.xml. How to Create a Maven Project with a pom.xml explains its first part, and the <build> section is new:

pom.xml for hello-exec with Java release 25 and a build section that adds exec-maven-plugin 3.7.0 with mainClass com.example.App.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>hello-exec</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass>com.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

And src/main/java/com/example/App.java:

App.java, whose main method takes the first argument as the name, falling back to world, and prints Hello with the name and Maven ran this with exec.

src/main/java/com/example/App.java
package com.example;

public class App {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "world";
System.out.println("Hello, " + name + "! Maven ran this with exec:java.");
}
}

Step 2: Run It​

Terminal
mvn -q compile exec:java
Output
Hello, world! Maven ran this with exec:java.

-q hides Maven's own messages, so only the program's output is left. The first run takes a few seconds longer while Maven downloads the plugins.

Step 3: Pass It Arguments​

Terminal
mvn -q compile exec:java "-Dexec.args=Ada"
Output
Hello, Ada! Maven ran this with exec:java.

A zsh terminal where mvn -q compile exec prints Hello, world! and the same command with "-Dexec.args=Ada" prints Hello, Ada! Maven ran this with exec.

The quotes matter in PowerShell. Without them, PowerShell splits -Dexec.args=Ada at the period and Maven stops with [ERROR] Unknown lifecycle phase ".args=Ada". Fixed: Unknown lifecycle phase ".args=Ada" When Running Maven in PowerShell shows why. Quoted, the same line works in zsh, bash and PowerShell. Separate several arguments with spaces inside the quotes: "-Dexec.args=Ada Lovelace" gives the program two arguments.

Common Mistakes​

MistakeWhat happensDo this instead
mvn exec:java without compile on a new projectMaven fails with An exception occurred while executing the Java class. com.example.AppRun mvn compile exec:java
-Dexec.args=Ada without quotes in PowerShellUnknown lifecycle phase ".args=Ada""-Dexec.args=Ada"
A mainClass without its packageMaven can't find the classWrite it in full: com.example.App

Troubleshooting​

SymptomLikely causeFix
An exception occurred while executing the Java class. com.example.AppThe class isn't compiled, or mainClass names a class that doesn't existAdd compile before exec:java, and check the name
[ERROR] Unknown lifecycle phase ".args=…"PowerShell split an unquoted -D argumentQuote the whole argument

FAQ​

What's the difference between exec:java and exec:exec?​

exec:java runs the class inside Maven's own Java process. exec:exec starts a separate program, such as a new java process, which suits programs that change system settings or need their own JVM options.

Can I leave mainClass out of pom.xml?​

Yes. Pass it instead, in quotes: mvn compile exec:java "-Dexec.mainClass=com.example.App". Putting it in pom.xml keeps the command short.

Conclusion​

Add exec-maven-plugin with a mainClass to pom.xml, and mvn -q compile exec:java compiles and runs the program. Pass it arguments with "-Dexec.args=…", quoted, so the same command works in every shell.

Additional Resources​