我用java编写了一个程序,我想在Windows中运行并显示以下命令(top -n 1,运行/bin/ls/)的结果。
String s = null;
ArrayList<String> outputs = new ArrayList<String>();
try {
// run the Unix command
Process p = Runtime.getRuntime().exec("top -n 1");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
outputs.add(s);
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
我收到以下错误
java.io.IOException: Cannot run program "top": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at code.ExecuteCommands.execute(ExecuteCommands.java:24)
at code.Server.process(Server.java:61)
at code.Server.main(Server.java:42)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 7 more
最佳答案
您正在尝试运行名为“top”的程序,该程序通常在 unix/linux/freebsd 操作系统下使用,用于显示进程列表和 CPU 使用情况输出。
Vanilla Windows 没有看起来像 top 的程序,但它有一个可以执行的名为“tasklist”的程序。输出不同,但您也许可以相同地使用它。
如果您希望能够调用看起来与top完全相同的东西,您可以尝试安装类似linux的环境,例如cygwin。
关于java - 将linux代码转换为windows代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20833256/