Complete Guide to Running Java Main Classes with Maven Exec Plugin

Nov 21, 2025 · Programming · 18 views · 7.8

Keywords: Maven | Java | Exec Plugin | Command Line Execution | Class Loading

Abstract: This article provides a comprehensive guide on using the Maven Exec plugin to run Java application main classes from the command line. It covers basic command-line usage, parameter passing, Windows system special handling, POM configuration methods, and analyzes class loading issues after Maven clean operations with practical case studies. Through in-depth analysis of plugin configuration and real-world application scenarios, it helps developers efficiently manage Java application execution processes.

Overview of Maven Exec Plugin

The Maven Exec plugin is a crucial tool in the Apache Maven ecosystem, specifically designed to execute Java classes and system commands during the Maven build process. This plugin offers flexible configuration options that enable developers to run applications without leaving the Maven environment, significantly improving development efficiency.

Basic Command Line Usage

To run a Java main class without parameters, use the following command format:

mvn exec:java -Dexec.mainClass="com.example.Main"

This command starts a new JVM instance to execute the specified main class. The -Dexec.mainClass parameter is used to specify the fully qualified class name, including the package path.

Parameter Passing Mechanism

When the application needs to receive command-line arguments, use the -Dexec.args parameter:

mvn exec:java -Dexec.mainClass="com.example.Main" -Dexec.args="arg0 arg1"

Arguments are separated by spaces, and the Exec plugin automatically passes these parameters to the main class's main method. This mechanism makes testing different parameter combinations very convenient.

Windows System Special Handling

In Windows operating systems, due to differences in command-line parsing, additional quote handling is required for parameters:

mvn exec:java -D"exec.mainClass"="com.example.Main"

This writing ensures that parameters are correctly parsed in the Windows command line, avoiding issues caused by spaces or special characters.

POM File Configuration Method

For applications that need frequent execution, permanent configuration in pom.xml is recommended:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>com.example.Main</mainClass>
    <arguments>
      <argument>foo</argument>
      <argument>bar</argument>
    </arguments>
  </configuration>
</plugin>

This configuration approach tightly integrates execution logic with project building, allowing execution to be triggered with a simple mvn exec:java command without needing to specify the class name and parameters each time.

Class Loading Issue Analysis and Resolution

In actual development, developers may encounter situations where the main class cannot be run after executing mvn clean. This is typically because the clean operation deletes compiled class files in the target/classes directory, and the IDE does not recompile in time.

Solutions include: manually refreshing the project, modifying build configuration to ensure automatic recompilation after clean, and checking if build path configurations are consistent. Some IDEs like IntelliJ IDEA can automatically detect and recompile, while others may require explicit configuration.

Best Practice Recommendations

When using the Maven Exec plugin, it's recommended to follow these best practices: keep the plugin version updated to ensure compatibility and security; unify configuration methods in team projects; properly handle dependencies to ensure complete execution environment; integrate with continuous integration tools for automated testing.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.