run main class of Maven project [duplicate]
Asked Answered
H

2

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
Hilliard answered 23/3, 2012 at 20:23 Comment(0)
H
610

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>
Hanoverian answered 23/3, 2012 at 20:28 Comment(17)
it is not convenient to run main class in this way:(Megaera
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).Sankhya
@Megaera 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.Trotta
@NathanielWaisbrot You can also add the parameters into there as well, I've edited the answer for this.Hanoverian
@NathanielWaisbrot Adding exec.mainClass under properties works for me too. I didn't need to tie it to any exec plugin version. Thanks!Swanherd
You can also add -Dexec.classpathScope=test if the class is in the test directoriesFormerly
You can also pass arguments using exec.args property, e.g., -Dexec.args=1 2 3. I found reading through the documentation helpful: mojo.codehaus.org/exec-maven-plugin/java-mojo.htmlOtherwise
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.Gorse
Is it maven-exec-plugin or exec-maven-plugin. It is confusing.Verbiage
In this same plugin, is there a way to specify maven to "compile" before running the mainClass?Sension
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 <properties><exec.mainClass> in pom.xml and running just mvn exec:java. I suppose I could also add <exec.args> for arguments. Tedious though.Caceres
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"Caceres
@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!?Strachan
check out this link for java9 modules : maven java 9Jequirity
@NathanielWaisbrot it gives me a ClassNotFoundException, I checked the package and classname and it is correctAwhirl
@Awhirl 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).Trotta
build is required before: mvn clean packageCaulescent
S
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.

Supremacy answered 29/3, 2012 at 9:40 Comment(4)
Note that this will not work out of the box on Windows which does not have the programs needed.Amabil
That's true. For windows you can always use cygwin, to get a *nix shell like, with grep, sed, cut ...Supremacy
Saved a lot of time, thnxDunaway
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 runSension

© 2022 - 2024 — McMap. All rights reserved.