2

Linuxサーバー上で特定の名前のすべてのファイルを検索し、それらの絶対パスをArrayListにパイプして処理するプログラムを作成しようとしています。Process(execを使用)とBufferedReaderを使用する私の方法は、他のすべてのニーズ(du -h df-hなどの他のさまざまなコマンド)で機能したようですが、これでは機能していないようです。データが出力されない場合!完了するまでに1〜2分かかるため、実行されているように見えますが、データの結果は表示されません。

コードは次のとおりです:(スタックトレースを出力するだけのtry/catchなし)

Process process = 
    Runtime.getRuntime().exec("find " + Main.serversPath + " -name 'dynmap'");
BufferedReader stdInput = new BufferedReader(
    new InputStreamReader(process.getInputStream()));

while ((s = stdInput.readLine()) != null) {
    filesToDelete.add(s);

    if (Main.debugMode == "High") {
        System.out.println("Preprocess: dynmap pass - found " + s);
    }
}

process.destroy();
4

3 に答える 3

1

この行:

System.out.println("Preprocess: dynmap pass - found " + s);

実行されない可能性があります。

文字列で使用すると、==演算子は値ではなくオブジェクト参照を比較します。

実際の値を比較するには、次のString.equals()メソッドを使用する必要があります。

if (Main.debugMode.equals("High")) {
    System.out.println("Preprocess: dynmap pass - found " + s);
}

Java でのオブジェクトの比較の概要については、こちらを参照してください

于 2012-12-20T02:06:48.670 に答える
1

プロセスがエラーをスローするケースを処理するために、(StreamGobbler を使用して) 別のスレッドでプロセスのエラー ストリームを常にキャプチャする必要があります。

class StreamGobbler extends Thread
{
    private InputStream is;
    private String myMessage;

    public StreamGobbler(InputStream istream)
    {
        this.is = istream;
    }

    public String getMessage()
    {
        return this.myMessage;
    }

    @Override
    public void run()
    {
        StringBuilder buffer = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        int size = 1024 * 1024;
        char[] ch = new char[size];
        int read = 0;
        try {
            while ((read = br.read(ch, 0, size)) >= 0) {
                buffer.append(ch, 0, read);
            }
        }
        catch (Exception ioe) {
            ioe.printStackTrace();
        }
        finally {
            try {
                br.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.myMessage = buffer.toString();
        return;
    }
}

次に、次のように StreamGobbler を使用してエラー ストリームをキャプチャする必要があります。

Process process = 
    new ProcessBuilder("find", Main.serversPath, "-name", "'dynmap'").start();

StreamGobbler error = new StreamGobbler(process.getErrorStream());
error.start();

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

while ((s = stdInput.readLine()) != null) {
    filesToDelete.add(s);

    if (Main.debugMode.equals("High")) {
        System.out.println("Preprocess: dynmap pass - found " + s);
    }
}

// Get the exit status
int exitStatus = process.waitFor();
if (exitStatus != 0) {
    // read the error.getMessage() and handle accordingly.
}
process.destroy();

また、ProcessBuilderを使用してプロセスを作成することをお勧めします。

于 2012-12-20T02:07:18.080 に答える
0

あなたの問題は、BufferedRreader を閉じていないことだと思います。常に閉じて、必要がある場合はストリームをフラッシュする必要があります。そう:

    Process process = Runtime.getRuntime().exec("find " + Main.serversPath + " -name 'dynmap'");
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

    while ((s = stdInput.readLine()) != null) {
        filesToDelete.add(s);

        if (Main.debugMode == "High") {
            System.out.println("Preprocess: dynmap pass - found " + s);
        }
    }
    process.destroy();
    //Add this line here!
    stdInput.close();

これを変更することもできます。

    if (Main.debugMode == "High") {
            System.out.println("Preprocess: dynmap pass - found " + s);
    }

これに:

   if (Main.debugMode.equals("High")) {
            System.out.println("Preprocess: dynmap pass - found " + s);
   }
于 2012-12-20T01:55:54.710 に答える