无法使用ProcessBuilder运行程序,从命令行运行正常 [英] Can't run program with ProcessBuilder, runs fine from command line
问题描述
在linux(debian)上,我可以运行这个命令:
On linux (debian), I can run this command:
/usr/lib/jvm/jdk1.7.0_21/bin/java -jar ~/myjar.jar ".*"
我正在尝试运行它来自Java程序而不是:
I am trying to run it from a Java program instead with:
ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", "\".*\"");
System.out.println(pb.command());
按预期打印以下内容:
System.out.println(pb.command());
prints the following, as expected:
[/usr/lib/jvm/jdk1.7.0_21/bin/java, -jar, ~/myjar.jar, ".*"]
但是我不喜欢从程序中获取相同的输出(它运行但输出看起来好像。*
参数没有被正确考虑)。
However I don't get the same output from the program (it runs but the ouput looks as if the ".*"
argument is not properly taken into account).
为什么它不起作用的任何想法?
Any ideas why it doesn't work?
注意:相同的代码在Windows上正常工作。
推荐答案
看起来没有使用 glob 。您可以改为使用shell:
Looks like the wildcard character is not being expanded using a glob. You can use a shell instead:
ProcessBuilder pb =
new ProcessBuilder("bash", "-c", "java -jar ~/myjar.jar \".*\"");
或者你可以删除通配符周围的双引号:
or you can remove the double-quotes around the wildcard:
ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", ".*");
这篇关于无法使用ProcessBuilder运行程序,从命令行运行正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!