[英]Can someone please explain to me why I get a different output when I run the same command from Java
When I run the command "pmset -g batt | egrep '([0-9]+\\%).*' -o --colour=auto | cut -f1 -d';' 当我运行命令“ pmset -g batt | egrep'([[0-9] + \\%)。*'-o --colour = auto | cut -f1 -d';' " in the OSX terminal, it outputs the battery percentage (Eg. 55%). 在OSX终端中,它输出电池百分比(例如55%)。
But when I run the same command in my Java code, I get "Currently drawing from 'Battery Power'" 但是,当我在Java代码中运行相同的命令时,我得到“当前正在从'电池电源'供电”
Here's how it looks in my Java code: 这是我的Java代码中的样子:
String cmd = "pmset -g batt | egrep '([0-9]+\\%).*' -o --colour=auto | cut -f1 -d';'";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
BufferedReader stdOutput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String output = stdOutput.readLine();
System.out.println(output);
I thought it had to do with the double backslash I'm using, but I checked and I don't think that's the reason. 我认为这与我使用的双反斜杠有关,但我检查了一下,但我不认为这是原因。
Thanks 谢谢
I think you need to read more from your Process, and you should use a ProcessBuilder . 我认为您需要从Process中了解更多信息,并且应该使用ProcessBuilder 。
for (;;) {
String output = stdOutput.readLine();
if (output == null) {
break;
}
System.out.println(output);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.