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
lospejos
  • 1,976
  • 3
  • 19
  • 35
Dónal
  • 185,044
  • 174
  • 569
  • 824

2 Answers2

605

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>
ADTC
  • 8,999
  • 5
  • 68
  • 93
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • 22
    it is not convenient to run main class in this way:( – hugemeow Sep 18 '12 at 10:40
  • 96
    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 '13 at 23:00
  • 71
    @hugemeow I added `com.waisbrot.MainClass` to my POM and now I can just run `mvn exec:java` That's not too bad. – Nathaniel Waisbrot Apr 11 '13 at 21:59
  • @NathanielWaisbrot You can also add the parameters into there as well, I've edited the answer for this. – Matthew Farwell May 16 '13 at 10:34
  • @NathanielWaisbrot Adding exec.mainClass under properties works for me too. I didn't need to tie it to any exec plugin version. Thanks! – Parag Doke Oct 20 '13 at 03:21
  • 22
    You can also add `-Dexec.classpathScope=test` if the class is in the test directories – M Smith Mar 07 '14 at 22:47
  • You can also pass arguments using `exec.args` property, e.g., `-Dexec.args=1 2 3`. I found reading through the documentation helpful: http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html – haridsv Jan 05 '15 at 05:26
  • Can the same thing be achieved with the help of run configurations. I thought it will create a new goal called "java" that I can select in run configurations. But it did not work that way. – shshnk Nov 25 '15 at 08:50
  • Is it maven-exec-plugin or exec-maven-plugin. It is confusing. – venky Feb 06 '17 at 02:02
  • In this same plugin, is there a way to specify maven to "compile" before running the mainClass? – Bikash Gyawali Mar 24 '17 at 09:40
  • I tried the full command and I got `[ERROR] Unknown lifecycle phase ".mainClass=com.example.Main". You must specify a valid lifecycle phase or a goal`. Not sure if it's because of Windows. But I fixed it by adding `` in `pom.xml` and running just `mvn exec:java`. I suppose I could also add `` for arguments. Tedious though. – ADTC Nov 27 '17 at 18:13
  • Aha! It's because of _Windows_. It works when I quote `exec.mainClass` on the command line, like so: `mvn exec:java -D"exec.mainClass"="com.example.Main"` – ADTC Nov 28 '17 at 18:28
  • @WarrenP, especially considering that I created the project through Maven in the first place. Why do I have to hold Maven's hand and tell it that yes, I'd actually like to *run* that project once in a while!? – Ryan Lundy Jul 02 '18 at 09:41
  • 1
    check out this link for java9 modules : [maven java 9](https://github.com/hakdogan/Java9-module-system-with-maven/blob/master/Java9ModuleSystem/pom.xml) – Martin Meeser May 02 '19 at 13:29
  • @NathanielWaisbrot it gives me a ClassNotFoundException, I checked the package and classname and it is correct – nerdguy Jul 13 '20 at 22:19
  • @nerdguy I recommend creating a new question where you can describe your setup in detail. In that question, you might reference this answer to say that it did not work for you (this will help people answer your question). – Nathaniel Waisbrot Jul 14 '20 at 18:10
  • build is required before: `mvn clean package` – JRichardsz Nov 27 '21 at 17:13
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.

albfan
  • 12,542
  • 4
  • 61
  • 80
  • 4
    Note that this will not work out of the box on Windows which does not have the programs needed. – Thorbjørn Ravn Andersen Dec 13 '12 at 09:24
  • That's true. For windows you can always use cygwin, to get a *nix shell like, with grep, sed, cut ... – albfan Dec 15 '12 at 13:14
  • Saved a lot of time, thnx – Gun2sh Nov 22 '15 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 – Bikash Gyawali Dec 14 '16 at 17:05