Getting Started with Maven | Miscellaneous | Running a Java Program

This article explains how to run a Java program in a Maven project, meaning a class with a main method, using the Exec Maven Plugin.

This time, instead of defining the plugin in pom.xml, we will pass the class name and arguments on the command line and run the program.

Preparing pom.xml Before Execution

Before running the command, create the project’s pom.xml as follows. The root directory is execplg.

pom.xml

execplg/pom.xml

<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
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.devkuma.mvn</groupId>
  <artifactId>execplg</artifactId>
  <version>1.0.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
</project>

Running the Main Class in the main Folder

1. Create the Main Class

execplg/src/main/java/com/devkuma/mvn/Main.java

package com.devkuma.mvn;

public class Main {
  public static void main(String[] args) {
   System.out.println("Class name:");
    System.out.println("  com.devkuma.mvn.Main");
    System.out.println("Arguments:");
    System.out.print("  arg0=");
    System.out.println(args[0]);
    System.out.print("  arg1=");
    System.out.println(args[1]);
  }
}

When executed, the command-line arguments are printed.

2. Compile

Because the Exec plugin (exec:java) does not compile the application, compile it before running the application if necessary.

> mvn compile

3. Run the Command

Run the following command from the project’s root directory.

$ mvn exec:java -Dexec.mainClass="com.devkuma.mvn.Main" -Dexec.args="argument1 argument2"

In this case, the com.devkuma.mvn.Main class runs with argument1 and argument2 as its arguments.

Running the TestMain Class in the test Folder

1. Create the Main Class

execplg/src/test/java/com/devkuma/mvn/TestMain.java

package com.devkuma.mvn;

public class TestMain {
  public static void main(String[] args) {
    System.out.println("Class name:");
    System.out.println("  com.devkuma.mvn.TestMain");
    System.out.println("Arguments:");
    System.out.print("  arg0=");
    System.out.println(args[0]);
  }
}

When executed, the command-line argument is printed.

2. Compile

Because the Exec plugin does not compile the application, run test-compile if necessary.

$ mvn test-compile

3. Run the Command

The following is an example command.

$ mvn exec:java -Dexec.mainClass=com.devkuma.mvn.TestMain -Dexec.classpathScope=test

Adding -Dexec.classpathScope=test as an option allows you to run classes under src/test/java.

About Goals

This article used exec:java, but there is also a goal named exec:exec. The main differences are as follows.

  • exec:java: Runs the specified Main class in the same VM as Maven.
  • exec:exec: Runs the specified Main class in a different VM from Maven.