299

Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".

6 Answers 6

438

See the exec maven plugin. You can run Java classes using:

mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...

The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <mainClass>com.example.Main</mainClass>
                    <arguments>
                        <argument>argument1</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
5
  • 26
    this is how an answer should look like! reference + simple example + complex example. the simple example did the trick for me (no more config needed)
    – codewing
    Commented Oct 11, 2016 at 9:28
  • 2
    is there a newer version of this plugin (newer than 1.2.1?) Commented Sep 22, 2017 at 22:44
  • 2
    Yes, there's a newer version, so don't copy the fragment above 1-to-1 unless you want to use version 1.2.1 See mojohaus.org/exec-maven-plugin/usage.html for the latest version As of now it's 1.6.0 Commented Oct 24, 2017 at 6:19
  • Thanks, would you also know how to run a main class if in submodule with dependencies in other modules ? Commented Dec 30, 2019 at 14:53
  • 1
    This does not actully run the main class
    – Dexter
    Commented May 20, 2020 at 8:44
67

1. Edit POM.xml

Add the following property in pom.xml. Make sure you use the fully qualified class name (i.e. with package name) which contains the main method:

<properties>
        <exec.mainClass>fully-qualified-class-name</exec.mainClass>
</properties>

2. Run Command

Now from the terminal, trigger the following command:

mvn clean compile exec:java

NOTE You can pass further arguments via -Dexec.args="xxx" flag.

4
  • 1
    Does this actually work? I've tried it as both: <exec.mainClass>${foo.bar.SomeMainClass}</exec.mainClass> and <exec.mainClass>foo.bar.SomeMainClass</exec.mainClass> and it doesn't work. Error is the same: [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project newtrex: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1] Commented Jun 27, 2019 at 12:02
  • It works, you may want to check this post
    – Saikat
    Commented Jun 27, 2019 at 14:48
  • 1
    @NenadBulatovic : It works if you substitute ${foo.bar.SomeMainClass} with foo.bar.SomeMainClass -> without the $ or { } Commented Mar 23, 2020 at 14:23
  • @NenadBulatovic the key is, add the @(myProjectId) after exec:java. To sum up: mvn clean compile exec:java@(myProjectId) Commented Nov 18, 2021 at 9:18
15

The above mentioned answers are correct but I am simplifying it for noobs like me.Go to your project's pom file. Add a new property exec.mainClass and give its value as the class which contains your main method. For me it was DriverClass in mainpkg. Change it as per your project. enter image description here

Having done this navigate to the folder that contains your project's pom.xml and run this on the command prompt mvn exec:java. This should call the main method.

0
14

No need to add new plugin in pom.xml. Just run this command

mvn org.codehaus.mojo:exec-maven-plugin:1.5.0:java -Dexec.mainClass="com.example.Main" | grep -Ev '(^\[|Download\w+:)' 

See the maven exec plugin for more usage.

1
  • 1
    Pass further arguments via -Dexec.args="...arg..."
    – sstn
    Commented Feb 28, 2018 at 9:08
6

Give the Exec Maven plugin a try

2

clean package exec:java -P Class_Containing_Main_Method command is also an option if you have only one Main method(PSVM) in the project, with the following Maven Setup.

Don't forget to mention the class in the <properties></properties> section of pom.xml :

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.main.class>com.test.service.MainTester</java.main.class>
</properties>

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
           <mainClass>${java.main.class}</mainClass>
        </configuration>
</plugin>

STS Run Configuration along with above Maven Setup:

enter image description here

2
  • That doesn't look right to me. Isn't -P for 'profile'? If you're specifying it on the command line why do you need to put it in the plugin properties?
    – Rup
    Commented Nov 23, 2015 at 19:23
  • @Rup Yes, -P is for profile. Shared this, as it is also an option of running Maven project.
    – Abhijeet
    Commented Nov 24, 2015 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.