简体   繁体   English

有人可以向我解释为什么当我从Java运行相同的命令时为什么得到不同的输出

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

相关问题 有人可以解释为什么我不能从方法 showMenu 中取回选择变量 - Could someone please explain why I can't get the selection variable back from the method showMenu 我不明白这段代码背后的逻辑。 有人可以向我解释它的真正作用和方式吗? - i do not get the logic behind this code. Can someone please explain to me what it really does and how? 有人可以解释为什么我在这段代码中收到错误 - Can someone please explain why I'm getting an error in this code 有人可以告诉我为什么我得到这个输出吗? - Can someone tell me why I get this output? 有人可以向我解释为什么我有此错误吗? - Can someone explain to me why I'm having this error? java 中的数据源是什么? 有人可以用简单的语言向我解释吗? - What is a datasource in java? Can someone please explain me in simple language? 有人可以告诉我这里的Java代码在做什么吗? - Can someone please explain to me what the java code here is doing? 有人可以帮助我编写我正在编写的代码吗? (爪哇) - Can someone please help me with the code I am writing? (Java) 有人可以解释一下hashmap get(key)方法的代码吗? - Can someone please explain me the code for the hashmap get(key) method? 我需要有人为我解释这个java代码 - I need someone to explain for me this java code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM