2

Java 内から Mac OSX でシステム コマンドを実行できるようにしたいと考えています。私のコードは次のようになります。

public void checkDisks() throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec("df -h");
    int exitValue = p.waitFor();
    System.out.println("Process exitValue:" + exitValue);


    BufferedReader reader = new BufferedReader(new InputStreamReader(
                                                 p.getInputStream()));
    String line = reader.readLine();
    while (line != null) {
        line = reader.readLine();
    }
    System.out.println(line);
}

これは常に null と 0 の exitValue を返します。これまで Java でこれを行ったことがないため、考えや提案は大歓迎です。

4

2 に答える 2

3

あなたのコードはほとんど問題ありません。println を置き忘れただけです。

public void checkDisks() throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec("df -h");
    int exitValue = p.waitFor();
    System.out.println("Process exitValue:" + exitValue);


    BufferedReader reader = new BufferedReader(new InputStreamReader(
                                                 p.getInputStream()));
    String line = reader.readLine();
    while (line != null) {
        line = reader.readLine();
        System.out.println(line);
    }
}

それがあなたが達成しようとしていることだと思います。

于 2013-10-14T09:04:19.463 に答える
1

これを試して

public void checkDisks() throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec(new String[]{"df","-h"});
    int exitValue = p.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
                                                 p.getInputStream()));
    String line;
    while ((line=reader.readLine()) != null) {
            System.out.println(line);
    }
    System.out.println("Process exitValue:" + exitValue);
}
于 2013-10-14T09:09:22.323 に答える