将参数传递给在bat文件中调用的jar文件?

发布于 2024-12-04 10:35:10 字数 142 浏览 4 评论 0原文

我创建了一个需要在bat 文件中调用的jar。我需要将 bat 文件收到的所有命令行参数传递给 jar。谁能帮我一下。我知道这是一个愚蠢的问题,但我对 jar 和 bat 没有任何想法。在网上我无法找到两者的组合。另请注意,我不知道如何检索 bat 文件中的命令行参数。

I have created a jar which needs to be called in a bat file. I need to pass all the command line arguments recieved by bat file to the jar. Can anyone please help me out. I know this is a stupid question, but i dont have any idea about jar and bat. On net i am unable to find the combination of both. Also note that i dont know how to retieve the command line arguments in the bat file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的快感 2024-12-11 10:35:10

您可以通过以下方式访问传递给批处理文件的参数,

%1 %2 %3 ...

因此,如果您像这样调用批处理

C:>application.bat param1 param2 param3

,则批处理文件中的 java 调用应如下所示:

@echo off
java -cp app.jar com.example.Main %1 %2 %3

The parameters that you pass to your batch file can be accessed via

%1 %2 %3 ...

So if you call your batch like

C:>application.bat param1 param2 param3

then your java call inside the batch file should look like:

@echo off
java -cp app.jar com.example.Main %1 %2 %3
玩物 2024-12-11 10:35:10

在你的bat文件中你将有java命令
只需使用 java -jar helloworld.jar firstParam secondaryParam
我相信您也可以使用,因为我们如何将参数传递给 MavenANT 等等

mybatchFile.bat -DfirstParam -DsecondParam

Inside you bat file you will have java command
just use java -jar helloworld.jar firstParam secondParam and
I believe you can also use because that how we pass params to Maven and ANT etc

mybatchFile.bat -DfirstParam -DsecondParam
土豪 2024-12-11 10:35:10

如果您不知道用户可以传递多少参数到批处理文件(如果有),您需要获取编号。无论用户传递了什么参数,只需在开始时将以下代码片段添加到您的 main 方法中

for (String s : args) {
    // Iterate through String array in Java (args list)
    System.out.println(s);
}

您可以将所有参数存储在 Arraylist 中以使用它并稍后对其进行迭代

If you don't know how many parameters the users could pass to the batch file (if any) you need to get the no. of arguments whatever the user has passed, simply add the following snippet to your main method at the start

for (String s : args) {
    // Iterate through String array in Java (args list)
    System.out.println(s);
}

You can store all the arguments in an Arraylist to use it and iterate over it later

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文