如何解决在Runtime.getRuntimeexec中带有2个可执行文件的空格
传递给命令的每个参数都应该是一个单独的String元素。
所以你的命令数组应该看起来更像是…
String[] a = new String[] {
"C:\path\that has\spaces\plink",
"-arg1",
"foo",
"-arg2",
"bar",
"path/on/remote/machine/iperf -arg3 hello -arg4 world"};
现在,每个元素将在程序args
变量中显示为单独的元素
我也极大地鼓励您使用ProcessBuilder
它,因为它更易于配置,并且不需要您在其中包装一些命令。"\"...\""
解决方法
我有一条命令需要在Java中按照以下方式运行:
C:\path\that has\spaces\plink -arg1 foo -arg2 bar "path/on/remote/machine/iperf -arg3 hello -arg4 world"
当路径没有空格时,此命令可以正常工作,但是当我有空格时,似乎无法正常工作。我尝试了以下事情,运行Java 1.7
String[] a = "C:\path\that has\spaces\plink","-arg1 foo","-arg2 bar","path/on/remote/machine/iperf -arg3 hello -arg4 world"
Runtime.getRuntime().exec(a);
以及
String[] a = "C:\path\that has\spaces\plink","path/on/remote/machine/iperf","-arg3 hello","-arg4 world"
Runtime.getRuntime().exec(a);
但是似乎两者都没有做任何事情。关于我在做什么错的任何想法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。