java - 将linux代码转换为windows代码

标签 java linux windows

我用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/

相关文章:

windows - Perl:如何将编码添加到 PAR 打包存档

Java 简单日期格式化程序显示错误值

java - Jenkins : Cannot run program "hg"

c++ - 为 sql loader 禁用 c++ 输出消息

linux - Bash 脚本 : perform actions based on file contents

windows - 在不同模块中 Access VBA Form Private Sub 调用 Public Function

c++ - 无法在 Windows 8 的 Cygwin 上编译基本的 C++ 程序

java - 如何在删除某个元素后重新绘制 GUI?

java - ClassNotFoundException:org.dom4j.DocumentException

linux - 阅读生成文件。符号的含义