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

前端 未结 3 1125
一生所求
一生所求 2020-12-02 05:49

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

相关标签:
3条回答
  • 2020-12-02 06:26

    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
    
    0 讨论(0)
  • 2020-12-02 06:27

    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"
    
    0 讨论(0)
  • 2020-12-02 06:34

    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'"
    
    0 讨论(0)
提交回复
热议问题