简体   繁体   English

Maven:如何从传递 arguments 的命令行运行 a.java 文件

[英]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.

相关问题 从命令行在Java程序中传递运行参数会产生错误 - Passing run arguments in a java program from command line gives an error 将-D参数作为文件传递给Java命令行 - Passing -D arguments to Java command line as file 如何从命令行通过vm参数运行Java项目? - How to run a java project with vm arguments from command line? 如何从命令行使用 arguments 运行 Java 方法? - How to run Java method with arguments from command line? 如何从线程的run方法内部的单独文件访问和/或读取命令行参数(Java套接字编程) - How to access and/or read command line arguments from a separate file inside the run method of a thread (java socket programming) 如何使用来自java程序的参数运行bat文件? (尝试使用命令行工具自动化) - How to run bat file with arguments from a java program? (Trying to automate using a Command Line Tool ) 如何使用文件中的不同命令行参数运行相同的Java程序 - How to run the same java program with different command line arguments from a file 如何编译基于Maven的Java项目以从命令行运行它 - How to compile Maven based Java project to run it from command line 使用命令行中的参数从JAR运行Maven项目 - Run maven project from JAR with arguments from command line 从命令行运行带有参数的Maven项目给出错误 - Run Maven project from command line with arguments give error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM