[英]Maven: How to run a .java file from command line passing arguments
I have the following problem.我有以下问题。 I would like to run
mvn
from command line for a Main.java
file.我想从命令行为
Main.java
文件运行mvn
。 Main.java
accepts a parameter. Main.java
接受一个参数。 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?另外,如何从与 Main.java 文件夹不同的文件夹执行该命令?
for example the Main.java
is located in my/java/program/Main.java
.例如
Main.java
位于my/java/program/Main.java
中。 What should I put in我应该放什么
mvn exec:java -Dexec.mainClass="what to put here?" -Dexec.args="arg0 arg1 arg2"
You could run: mvn exec:exec -Dexec.args="arg1"
. 您可以运行:
mvn exec:exec -Dexec.args="arg1"
。
This will pass the argument arg1 to your program. 这会将参数arg1传递给您的程序。
You should specify the main class fully qualified, for example, a Main.java that is in a package test would need 您应该指定完全合格的主类,例如,在包测试中的Main.java将需要
mvn exec:java -Dexec.mainClass=test.Main
By using the -f
parameter, as decribed here , you can also run it from other directories. 通过使用
-f
参数(如此处所述) ,您还可以从其他目录运行它。
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. 对于以空格分隔的参数,可以在引号内使用
'argument separated with space'
进行分组。
mvn exec:java -Dexec.mainClass=test.Main -Dexec.args="'argument separated with space' 'another one'"
除了使用mvn exec:java
运行它之外,您还可以使用mvn exec:exec
运行它
mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath your.package.MainClass"
Adding a shell script eg run.sh
makes it much more easier: 添加一个shell脚本(例如
run.sh
使其变得更加容易:
#!/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
Take a look at the maven-exec-plugin . 看看maven-exec-plugin 。 Used properly, you can make it compile your java class and then simply execute java with the compiled class on the classpath.
如果使用得当,您可以编译java类,然后在类路径上使用编译后的类执行java。 To launch, all you would have to do is use
要启动,您只需使用即可
mvn exec:exec
this aproach is half line command, half.pom file.这种方法是半行命令,half.pom 文件。 you can put your args in a plugin inside the
<build> </build>
tag like this你可以把你的参数放在
<build> </build>
标签内的插件中,像这样
<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.现在你的 arg 在 pom 文件中。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.