105

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"
Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
phedon rousou
  • 1,740
  • 5
  • 17
  • 24
  • 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. – Perception Apr 11 '12 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) – phedon rousou Apr 11 '12 at 15:10

4 Answers4

174

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'"
Community
  • 1
  • 1
Behe
  • 7,572
  • 3
  • 33
  • 46
  • 1
    yes, but how does it knows where the main.java file is located? – phedon rousou Apr 11 '12 at 15:23
  • 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. – phedon rousou Apr 11 '12 at 16:05
  • 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. – Behe Apr 11 '12 at 16:21
  • 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 – phedon rousou Apr 11 '12 at 16:27
  • How to pass arguments that contain spaces? – Vanuan Nov 01 '13 at 15:30
  • @Vanuan: I updated my answer, you can put the arguments with spaces in single quotes – Behe Nov 01 '13 at 17:24
  • Maven is removing my trailing double quote, failing with error org.apache.maven.plugin.MojoExecutionException: unbalanced quotes in "-Darg=value – vadim Aug 12 '15 at 12:25
  • Hard to tell what is going on... Please consider to post a new question and describe your problem in detail! – Behe Aug 12 '15 at 13:50
  • ok. Let say I send argument `-Dexec.args="arg1 arg2 arg3"` , so, how to print the three arguments in the code ? it will store in what variable? – Syamsoul Azrien Jan 10 '18 at 09:33
11

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
Tommy1005
  • 111
  • 1
  • 3
  • This works great! Can you explain why this does not: -Dexec.args="$@" – baumato Oct 18 '19 at 09:32
  • 2
    `$@` 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? – Tommy1005 Jan 09 '20 at 11:37
8

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"
Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42
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>
Giovanni Contreras
  • 2,345
  • 1
  • 13
  • 22