Can't run program with ProcessBuilder, runs fine from command line

≡放荡痞女 提交于 2019-12-04 05:21:31

问题


On linux (debian), I can run this command:

/usr/lib/jvm/jdk1.7.0_21/bin/java -jar ~/myjar.jar ".*"

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()); 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.


回答1:


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", ".*");



回答2:


For. e.g. to get all the cpu frequency in Android.

ProcessBuilder p = new ProcessBuilder(); p.command("/system/bin/sh","-c","cat sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq ");



来源:https://stackoverflow.com/questions/16403539/cant-run-program-with-processbuilder-runs-fine-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!