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

4

107

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"
Marya answered 11/4, 2012 at 14:47 Comment(2)
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.Tasset
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)Marya
F
177

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'"
Foregather answered 11/4, 2012 at 15:10 Comment(9)
yes, but how does it knows where the main.java file is located?Marya
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.Marya
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.Foregather
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 itMarya
How to pass arguments that contain spaces?Etherealize
@Vanuan: I updated my answer, you can put the arguments with spaces in single quotesForegather
Maven is removing my trailing double quote, failing with error org.apache.maven.plugin.MojoExecutionException: unbalanced quotes in "-Darg=valueCadmus
Hard to tell what is going on... Please consider to post a new question and describe your problem in detail!Foregather
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?Doris
O
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
Ormiston answered 2/6, 2019 at 7:25 Comment(2)
This works great! Can you explain why this does not: -Dexec.args="$@"Oringas
$@ 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?Ormiston
D
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"
Defunct answered 12/1, 2016 at 14:13 Comment(3)
Where would I put the Xmx argument in this case ?Murrumbidgee
Try putting it into -Dexec.args: mvn exec:exec -Dexec.executable="java" -Dexec.args="-Xmx4g -classpath %classpath your.package.MainClass"Levitus
I don't see where this will be better than the (IMO) straight forward approach of exec:javaViolate
N
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>
Norry answered 3/4, 2022 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.