[英]Can't run program with ProcessBuilder, runs fine from command line
On linux (debian), I can run this command: 在linux(debian)上,我可以运行以下命令:
/usr/lib/jvm/jdk1.7.0_21/bin/java -jar ~/myjar.jar ".*"
I am trying to run it from a Java program instead with: 我正在尝试从Java程序运行它,而不是:
ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", "\".*\"");
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? 任何想法为什么不起作用?
Note: the same code works fine on Windows. 注意:相同的代码在Windows上可以正常工作。
Looks like the wildcard character is not being expanded using a glob . 看起来通配符没有使用glob进行扩展。 You can use a shell instead: 您可以改用Shell:
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", ".*");
For. 对于。 eg to get all the cpu frequency in Android. 例如,获取Android中所有的CPU频率。
ProcessBuilder p = new ProcessBuilder(); p.command("/system/bin/sh","-c","cat sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq ");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.