Run a Java Class from Maven made simple

In order to run a Java main class from your Maven project you can use the Maven exec plugin. In this short article we will learn how to run it at your fingertips.

Run Java Class from Maven with one liner

The maven exec plugin supports the following goals:

  • exec:exec executes programs and Java programs in a separate process.
  • exec:java executes Java programs in the same VM.

Therefore, the simplest way to run a Java class using Maven’s JVM is the following one:

mvn exec:java -Dexec.mainClass="com.sample.MainClass"  

To pass arguments to the Java main class, use -Dexec.args as follows:

mvn exec:java -Dexec.mainClass="com.sample.MainClass" -Dexec.args="arg0 arg1 arg2"   

Important: : As said, the java goal runs in the same JVM process. Therefore, any JVM option that you want to pass to the executed class requires the MAVEN_OPTS environment variable.

Adding the exec plugin in your pom.xml

If you prefer, you can add the Maven plugin into your configuration. This can be a handy solution if you want to run a Java class as part of a goal. For example, in the following example, you are binding the Java class execution with the test goal:

 <plugins>  
  <plugin>  
   <groupId>org.codehaus.mojo</groupId>  
   <artifactId>exec-maven-plugin</artifactId>  
   <version>3.0.0</version>  
   <executions>  
    <execution>  
     <phase>test</phase>  
     <goals>  
      <goal>java</goal>  
     </goals>  
     <configuration>  
      <mainClass>com.sample.MainClass</mainClass>  
      <arguments>  
       <argument>argument1</argument>  
      </arguments>  
     </configuration>  
    </execution>  
   </executions>  
  </plugin>  
 </plugins> 

To run the com.sample.MainClass just execute:

mvn test

Managing Classpath

The maven exec plugin automatically builds the classpath using all project dependencies,
including the project build directory. That is equivalent to using:

  <arguments>
    <argument>-classpath</argument>
    <classpath/>
    <argument>com.sample.MainClass</argument>
    ...
  </arguments>

On the other hand, if you want to specify the Classpath of your Class, you need to add manually the list of dependencies:

<argument>-classpath</argument>
<classpath>
  <dependency>commons-io:commons-io</dependency>
  <dependency>commons-lang:commons-lang</dependency>
</classpath>

Advanced configuration settings

Within your configuration settings, you can specify some optional settings, such as the workingDirectory where the task is executed, the JVM arguments or environmentVariables. Here is an example:

<configuration>
  <executable>maven</executable>
  <workingDirectory>/home/jboss</workingDirectory>
  <arguments>
     <argument>-Djava.library.path=${project.build.directory}/lib</argument>
  </arguments>
  <environmentVariables>
    <LANG>en_US</LANG>
    <MYVAR>value</MYVAR> 
  </environmentVariables>
</configuration>

Also, be aware that the configuration settings will be overridden from the Command Line options:

mvn exec:exec -DargumentA=alternateA -DargumentB=alternateB

Running the exec plugin within Eclipse

The problem is that the m2e plugin (which integrates Maven with Eclipse) is now aware of exec-maven-plugin. To solve it, you need to add the following plugin configuration:

   <build>
      <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>exec-maven-plugin</artifactId>
                                        <versionRange>[3.0.0,)</versionRange>
                                        <goals>
                                            <goal>java</goal>
                                            <goal>exec</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore/>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
   </build>

You can now run the Java class from Eclipse:

maven exec java class

You can learn more about Maven by checking our Apache Maven Faqs