使用Java运行Unix命令

时间:2017-03-22 13:36:29

标签: java unix cygwin processbuilder

我一直在尝试在java中显示简单的unix命令的输出(使用cygwin)。我读过,与cmd不同,unix shell不提供终端功能,所以我安装了cygwin,但我仍然无法执行此操作。这是我的代码,我试图从ps -ef打印一行的输出。当前,我收到"无法运行' ps -ef':没有这样的文件或目录"

try {
   Process process = Runtime.getRuntime().exec(new String[] { "C:\\cygwin64\\bin\\mintty.exe",  "ps -ef" });

    //process.waitFor();
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

    System.out.println("A line from ps -ef command:\n");                  
    s = stdInput.readLine();
    System.out.println(s);             
    System.exit(0);
} catch (IOException e1) {
    System.out.println("exception happened");
    e1.printStackTrace();
    System.exit(-1);
}

0 个答案:

没有答案