108

I have the following problem. I would like to run mvn from command line for a Main.java file. Main.java accepts a parameter. How do I do that from command line?

I tried finding an example but I was not successful. Could someone help me by giving me an example of that?

I looked here but didn't quite understand what I should do.

Also, how do I execute that command from a different folder than the Main.java folder?

for example the Main.java is located in my/java/program/Main.java. What should I put in

mvn exec:java -Dexec.mainClass="what to put here?" -Dexec.args="arg0 arg1 arg2"
2
  • 4
    What exactly did you not understand from the linked tutorial? Its pretty straight-forward. Please add to your question, the code you have tried so far. Commented Apr 11, 2012 at 14:54
  • Basically what I am trying to do is to call a java class from another java class. Normally I run that class from Eclipse. I am using Runtime.getRuntime().exec(""); to execute that class from another java program. But Main.class needs mvn to run. (I edited the question) Commented Apr 11, 2012 at 15:10

4 Answers 4

182

You could run: mvn exec:exec -Dexec.args="arg1".

This will pass the argument arg1 to your program.

You should specify the main class fully qualified, for example, a Main.java that is in a package test would need

mvn exec:java  -Dexec.mainClass=test.Main

By using the -f parameter, as decribed here, you can also run it from other directories.

mvn exec:java -Dexec.mainClass=test.Main -f folder/pom.xm

For multiple arguments, simply separate them with a space as you would at the command line.

mvn exec:java -Dexec.mainClass=test.Main -Dexec.args="arg1 arg2 arg3"

For arguments separated with a space, you can group using 'argument separated with space' inside the quotation marks.

mvn exec:java -Dexec.mainClass=test.Main -Dexec.args="'argument separated with space' 'another one'"
Sign up to request clarification or add additional context in comments.

9 Comments

yes, but how does it knows where the main.java file is located?
what if I do not have the pom.xml. I get the following error Cannot execute mojo: java. It requires a project with an existing pom.xml, but the build is not using one.
A maven project requires a pom.xml, without this file, using maven makes little sense. So, maybe you want to create a maven project first? Then all the other solutions should work just fine.
Because I use Eclipse with the maven plugin i thought it was generated automatically but apparently I was wrong. I will take a look into it
How to pass arguments that contain spaces?
|
12

Adding a shell script e.g. run.sh makes it much more easier:

#!/usr/bin/env bash
export JAVA_PROGRAM_ARGS=`echo "$@"`
mvn exec:java -Dexec.mainClass="test.Main" -Dexec.args="$JAVA_PROGRAM_ARGS"

Then you are able to execute:

./run.sh arg1 arg2 arg3

2 Comments

This works great! Can you explain why this does not: -Dexec.args="$@"
$@ stores all the arguments in a list of string wrapped in quotes. If you want to use it directly you may use $* - all arguments as a single string. Did you try this?
9

In addition to running it with mvn exec:java, you can also run it with mvn exec:exec

mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath your.package.MainClass"

3 Comments

Where would I put the Xmx argument in this case ?
Try putting it into -Dexec.args: mvn exec:exec -Dexec.executable="java" -Dexec.args="-Xmx4g -classpath %classpath your.package.MainClass"
I don't see where this will be better than the (IMO) straight forward approach of exec:java
0

this aproach is half line command , half .pom file . you can put your args in a plugin inside the <build> </build> tag like this

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <mainClass>%classpath your.package.MainClass</mainClass>
        <arguments>
            <argument>your_arg</argument>
        </arguments>
    </configuration>
</plugin>

now your arg is in the pom file . then just execute this in the command line

mvn clean compile exec:java

you can put many args :

       <arguments>
            <argument>your_arg1</argument>
            <argument>your_arg2</argument>
            <argument>your_arg3</argument>
        </arguments>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.