1

Javaアプリケーションからプロセスを実行しようとしています。コンソールからこのプロセスを実行すると、正しく機能しますが、getRuntime()。exec()を実行すると、開始されますが終了しません。例外も終了値もありません。私が実行しようとしているプロセスは、PDFファイルをPostScriptに変換するアプリであるpdftops.exeです。小さなファイルを変換しようとすると(Javaから実行)、問題なく動作します。問題は、より長い時間(20〜60秒)かかる可能性のある大きなPDFの変換です。実行時間が長すぎることが問題だと思います。プログラムを呼び出すコードは次のとおりです(コマンドラインが簡略化され、input.pdfとoutput.psがホームディレクトリ内のフォルダーに配置され、pdftops.exeがデスクトップに配置されます)。

String comando = "pdftops.exe input.pdf output.ps";
System.out.println("Executing "+comando);
try {
    Process pr = Runtime.getRuntime().exec(comando);            
    pr.waitFor();      
    System.out.println("Finished");
}
catch (IOException ex){
    ex.printStackTrace();
}
catch(InterruptedException ex){
    ex.printStackTrace();
}

編集:プロセスのErrorStreamを読み取ると、問題が解決します。

try {
    System.out.println(comando);
    Process process = Runtime.getRuntime().exec(comando);            

    String line;

    InputStream stderr = process.getErrorStream ();

    BufferedReader reader = new BufferedReader (new InputStreamReader(stderr));

    line = reader.readLine();
    while (line != null && ! line.trim().equals("--EOF--")) {
        System.out.println ("Stdout: " + line);
        line = reader.readLine();
    }
}
catch (IOException ex){
    ex.printStackTrace();
}
4

3 に答える 3

4

質問に対する即時の回答ではありませんが、プロセスのエラー/出力ストリームをキャプチャして、そこで何が起こっているかを把握するのに役立つ場合があります(何かが生成されると仮定します)。

java 7を使用すると、非常に便利なProcessBuilderを使用して、エラーストリームを出力ストリームにマージできます...

たとえば、何らかの入力を待っている可能性がありますか?

于 2012-12-18T10:09:50.917 に答える
2

私はProcessBuilderを使用します(Janが以前に言ったことと同様です) 。Java5を使用している場合、少なくとも以下のようなもので、エラーがあればそれがわかります...

public void execute () throws IOException, InterruptedException
{
    ProcessBuilder pb = new ProcessBuilder("pdftops.exe", "input.pdf", "output.ps");

    Process process = pb.start();

    System.out.println("Error stream:");
    InputStream errorStream = process.getErrorStream();
    printStream(errorStream);

    process.waitFor();

    System.out.println("Output stream:");
    InputStream inputStream = process.getInputStream();
    printStream(inputStream);
}

private void printStream (InputStream stream) throws IOException
{
    BufferedReader in = new BufferedReader(new InputStreamReader(stream));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
}
于 2012-12-18T10:23:31.453 に答える
0

Javaのドキュメントを読むと、次のことがわかります。

waitFor : Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.
Returns: the exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination.
Throws: InterruptedException - if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown.

私の推測では、大きなファイルの場合、実行を混乱させる時間の難問に遭遇します。おそらく、ここで次のように実行中のプロセスの終了状態を確認できます。

  if(Process.exitValue()==0)
  break;

これにより、実行が終了すると、無限またはオーバーアグレッシブなループが実行の最終結果にならないようになります。

于 2012-12-18T10:12:46.730 に答える