Maven:如何从命令行传递参数运行 .java 文件 [英] Maven: How to run a .java file from command line passing arguments

查看:64
本文介绍了Maven:如何从命令行传递参数运行 .java 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

此外,如何从 Main.java 文件夹以外的其他文件夹执行该命令?

例如 Main.java 位于 my/java/program/Main.java.我应该放什么

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"

推荐答案

您可以运行:mvn exec:exec -Dexec.args="arg1".

这会将参数 arg1 传递给您的程序.

This will pass the argument arg1 to your program.

您应该指定完全限定的主类,例如,包测试中的 Main.java 将需要

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

通过使用 -f 参数,如这里,你也可以从其他目录运行它.

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

这篇关于Maven:如何从命令行传递参数运行 .java 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆