Java 带参数执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2144165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute with parameters
提问by Dan Polites
I'm having difficulty executing a batch file in Java that expects parameters. These parameters may contain spaces so I need to wrap them in quotes. I will also need to do the same thing for Linux because some of the parameters may contain special characters such as !
.
我在 Java 中执行需要参数的批处理文件时遇到困难。这些参数可能包含空格,因此我需要将它们用引号括起来。我还需要为 Linux 做同样的事情,因为某些参数可能包含特殊字符,例如!
.
Non-functional Windows code:
非功能性 Windows 代码:
ProcessBuilder pb = new ProcessBuilder(
"cmd",
"/c",
"\"mybat.bat\"",
"\"param 1\"",
"\"param 2\"",
"\"param 3\""
);
Non-functional Linux code:
非功能性 Linux 代码:
ProcessBuilder pb = new ProcessBuilder(
"bash",
"-c",
"'myshellscript.sh'",
"'param 1'",
"'param 2'",
"'param 3'"
);
I understand that I should be adding the parameters like the Windows example below, but this won't work with the spaces:
我知道我应该像下面的 Windows 示例一样添加参数,但这不适用于空格:
ProcessBuilder pb = new ProcessBuilder(
"cmd",
"/c",
"mybat.bat param 1 param 2 param 3"
);
How should this be done?
这应该怎么做?
采纳答案by Gladwin Burboz
Windows:
视窗:
ProcessBuilder pb = new ProcessBuilder(
"cmd", "/c", "mybat.bat",
"param 1", "param 2", "param 3");
Unix:
Unix:
ProcessBuilder pb = new ProcessBuilder(
"sh", "mybat.sh",
"param 1", "param 2", "param 3");
回答by nobody
No, you should notquote the args on *nix. Quoting is necessary on *nix in an interactive shell to prevent the shell misinterpreting them, but when launching a process directly a shell isn't involved. Hence no need to quote.
不,你应该不报价* nix上的ARGS。在交互式 shell 中引用 *nix 是必要的,以防止 shell 误解它们,但是当直接启动进程时,不涉及 shell。因此无需引用。
If you do include the quotes, the launched process will see them as part of its incoming arguments and do things like (for example) try to open filenames containing quotes.
如果您确实包含引号,则启动的进程会将它们视为其传入参数的一部分,并执行诸如(例如)尝试打开包含引号的文件名之类的操作。
You also do not want the "-c" argument to bash. That tells it to parse the next argument as a command line, but you're supplying a list of arguments. Remove the "-c" option and the excess quotes and it should work.
您也不希望 bash 使用“-c”参数。这告诉它将下一个参数解析为命令行,但您提供的是一个参数列表。删除“-c”选项和多余的引号,它应该可以工作。
The proper Linux call would be:
正确的 Linux 调用是:
ProcessBuilder pb = new ProcessBuilder(
"bash",
"myshellscript.sh",
"param 1",
"param 2",
"param 3"
);
Also not that if the file "myshellscript.sh" is executable and has the appropriate shebang line (e.g. "#!/bin/bash"), you do not need the "bash" argument either. This is preferrable because if the script is ever replaced with one written in a different language you won't have to update your calling app.
也不是如果文件“myshellscript.sh”是可执行的并且有适当的shebang行(例如“#!/bin/bash”),你也不需要“bash”参数。这是可取的,因为如果脚本被替换为用不同语言编写的脚本,您将不必更新您的呼叫应用程序。
Likewise, on Windows, you shouldn't need the "cmd" and "/c" arguments. The process launcher / OS should handle launching the batch file (based on extension) automatically.
同样,在 Windows 上,您不应该需要“cmd”和“/c”参数。进程启动器/操作系统应该自动处理启动批处理文件(基于扩展名)。