Maven:如何从命令行传递参数运行.java文件

时间:2021-10-22 23:16:53

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?

我有以下问题。我想从命令行为Main.java文件运行mvn。 Main.java接受一个参数。我如何从命令行执行此操作?

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. What should I put in

例如,Main.java位于我的/ java / program / Main.java中。我应该放什么

mvn exec:java -Dexec.mainClass="what to put here?" -Dexec.args="arg0 arg1 arg2"

3 个解决方案

#1


113  

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.

对于用空格分隔的参数,可以使用引号内的“用空格分隔的参数”进行分组。

mvn exec:java -Dexec.mainClass=test.Main -Dexec.args="'argument separated with space' 'another one'"

#2


3  

In addition to running it with mvn exec:java, you can also run it with mvn exec:exec

除了使用mvn exec:java运行它之外,您还可以使用mvn exec:exec运行它

mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath your.package.MainClass"

#3


0  

Take a look at the 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. To launch, all you would have to do is use

看看maven-exec-plugin。如果使用得当,您可以使它编译您的java类,然后只需在类路径上使用已编译的类执行java。要启动,您只需使用即可

mvn exec:exec

#1


113  

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.

对于用空格分隔的参数,可以使用引号内的“用空格分隔的参数”进行分组。

mvn exec:java -Dexec.mainClass=test.Main -Dexec.args="'argument separated with space' 'another one'"

#2


3  

In addition to running it with mvn exec:java, you can also run it with mvn exec:exec

除了使用mvn exec:java运行它之外,您还可以使用mvn exec:exec运行它

mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath your.package.MainClass"

#3


0  

Take a look at the 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. To launch, all you would have to do is use

看看maven-exec-plugin。如果使用得当,您可以使它编译您的java类,然后只需在类路径上使用已编译的类执行java。要启动,您只需使用即可

mvn exec:exec