Maven: How to run a .java file from command line passing arguments

JavaMavenArguments

Java Problem Overview


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"

Java Solutions


Solution 1 - Java

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'"

Solution 2 - Java

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

Solution 3 - Java

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"

Solution 4 - Java

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionphedon rousouView Question on Stackoverflow
Solution 1 - JavaBeheView Answer on Stackoverflow
Solution 2 - JavaTommy1005View Answer on Stackoverflow
Solution 3 - JavaBenedikt KöppelView Answer on Stackoverflow
Solution 4 - JavaGiovanni ContrerasView Answer on Stackoverflow