296

I've created a simple console Java application that is built with Maven. Is there a way that the main class (which doesn't require any arguments) can be run from the command-line using a maven command like:

mvn run-app com.example.MainClass
0

2 Answers 2

606

Try the maven-exec-plugin. From there:

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

This will run your class in the JVM. You can use -Dexec.args="arg0 arg1" to pass arguments.

If you're on Windows, apply quotes for exec.mainClass and exec.args:

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

If you're doing this regularly, you can add the parameters into the pom.xml as well:

<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>
17
  • 23
    it is not convenient to run main class in this way:(
    – hugemeow
    Sep 18, 2012 at 10:40
  • 98
    Am I the only one who wishes maven had a built in convention where you could type in "mvn run" and your main program's main class would automatically run without you telling maven details it should already know (like what your main class is).
    – Warren P
    Mar 29, 2013 at 23:00
  • 71
    @hugemeow I added <properties><exec.mainClass>com.waisbrot.MainClass</exec.mainClass></properties> to my POM and now I can just run mvn exec:java That's not too bad. Apr 11, 2013 at 21:59
  • 22
    You can also add -Dexec.classpathScope=test if the class is in the test directories
    – M Smith
    Mar 7, 2014 at 22:47
  • 1
    check out this link for java9 modules : maven java 9 May 2, 2019 at 13:29
16

Although maven exec does the trick here, I found it pretty poor for a real test. While waiting for maven shell, and hoping this could help others, I finally came out to this repo mvnexec

Clone it, and symlink the script somewhere in your path. I use ~/bin/mvnexec, as I have ~/bin in my path. I think mvnexec is a good name for the script, but is up to you to change the symlink...

Launch it from the root of your project, where you can see src and target dirs.

The script search for classes with main method, offering a select to choose one (Example with mavenized JMeld project)

$ mvnexec 
 1) org.jmeld.ui.JMeldComponent
 2) org.jmeld.ui.text.FileDocument
 3) org.jmeld.JMeld
 4) org.jmeld.util.UIDefaultsPrint
 5) org.jmeld.util.PrintProperties
 6) org.jmeld.util.file.DirectoryDiff
 7) org.jmeld.util.file.VersionControlDiff
 8) org.jmeld.vc.svn.InfoCmd
 9) org.jmeld.vc.svn.DiffCmd
10) org.jmeld.vc.svn.BlameCmd
11) org.jmeld.vc.svn.LogCmd
12) org.jmeld.vc.svn.CatCmd
13) org.jmeld.vc.svn.StatusCmd
14) org.jmeld.vc.git.StatusCmd
15) org.jmeld.vc.hg.StatusCmd
16) org.jmeld.vc.bzr.StatusCmd
17) org.jmeld.Main
18) org.apache.commons.jrcs.tools.JDiff
#? 

If one is selected (typing number), you are prompt for arguments (you can avoid with mvnexec -P)

By default it compiles project every run. but you can avoid that using mvnexec -B

It allows to search only in test classes -M or --no-main, or only in main classes -T or --no-test. also has a filter by name option -f <whatever>

Hope this could save you some time, for me it does.

4
  • 4
    Note that this will not work out of the box on Windows which does not have the programs needed. Dec 13, 2012 at 9:24
  • That's true. For windows you can always use cygwin, to get a *nix shell like, with grep, sed, cut ...
    – albfan
    Dec 15, 2012 at 13:14
  • Saved a lot of time, thnx
    – Momin
    Nov 22, 2015 at 21:22
  • Gives error : Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (default-cli) on project srl: Execution default-cli of goal org.codehaus.mojo:exec-maven-plugin:1.5.0:exec failed: Executable can not be empty -> [Help 1] even though I specified the number of the main class to run Dec 14, 2016 at 17:05

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