简体   繁体   English

无法使用ProcessBuilder运行程序,可以从命令行正常运行

[英]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.

相关问题 程序在Eclipse中运行,不在命令行上运行 - Program runs in Eclipse, doesn't run at command line 无法运行ProcessBuilder - Can't run ProcessBuilder Spring Boot应用程序无法在STS中运行,但在命令行上可以正常运行 - Spring boot app fails to run in STS but runs fine on command line 为什么Java Processbuilder不能在其自己的目录中运行PMCMD(informatica命令),但可以在命令提示符下正常运行 - Why PMCMD (informatica command) is not run by Java Processbuilder from its own directory while it works fine with command prompt Java ProcessBuilder() 运行一个程序,但该程序不返回任何输出 - Java ProcessBuilder() runs a program, but the program doesn't return any output 无法从命令行运行swing - can't run swing from the command line 无法使用包从命令行运行多类程序 - Can't run multiple-class program from command line using packages 我可以从Eclipse创建的命令行程序运行吗? - Can I run from command line program created by Eclipse? 当我在 VSCode 中运行时程序运行良好,但是 package maven 扩展构建不运行 - Program runs fine when I run in VSCode, but the package the maven extension builds doesn't run 无法使用ProcessBuilder运行Shell脚本 - Can't run shell script with ProcessBuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM