我想从 java 运行 windows 命令行命令并将结果返回到 java。这可能吗?
例如,我想做以下
Object returnValue = runOnCommandLine("wmic cpu get LoadPercentage"); //在这种情况下,returnValue 是字符串形式的 cpu 负载百分比
编辑:我能够得到这个工作
InputStream inputStream = new ProcessBuilder("wmic", "cpu", "get", "status").start().getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
String theString = writer.toString();
System.out.println("My string: " + theString);