Java进程从不停止

Java进程从不停止,java,process,Java,Process,我试图从Java应用程序执行一个进程,当我从控制台执行这个进程时,它工作正常,但当我执行getRuntime().exec()时,它开始但永远不会结束,没有异常,没有退出值。 我尝试执行的过程是pdftops.exe,一个将PDF文件转换为PostScript的应用程序。 当我尝试转换小文件(从Java执行)时,它工作正常,问题是转换更大的PDF可能需要更长的时间(从20秒到60秒)。我认为问题可能是执行时间太长。 以下是调用该程序的代码(命令行简化,input.pdf和output.ps放在我

我试图从Java应用程序执行一个进程,当我从控制台执行这个进程时,它工作正常,但当我执行getRuntime().exec()时,它开始但永远不会结束,没有异常,没有退出值。 我尝试执行的过程是pdftops.exe,一个将PDF文件转换为PostScript的应用程序。 当我尝试转换小文件(从Java执行)时,它工作正常,问题是转换更大的PDF可能需要更长的时间(从20秒到60秒)。我认为问题可能是执行时间太长。 以下是调用该程序的代码(命令行简化,input.pdf和output.ps放在我的主目录中的文件夹中,pdftops.exe放在桌面中):

编辑:读取流程的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();
}

这不是对您的问题的直接回答,但可能有助于捕获流程的错误/输出流,以便您知道那里发生了什么(假设它产生了什么)

使用Java7,您可以非常方便地将错误流合并到输出流中


例如,它可能正在等待输入吗?

不是对您的问题的即时回答,但可能有助于捕获流程的错误/输出流,以便您知道那里发生了什么(假设它产生了什么)

使用Java7,您可以非常方便地将错误流合并到输出流中


例如,它可能正在等待输入吗?

如果浏览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;

这将确保执行结束后,无限或过度的循环不会成为执行的最终结果。

如果您阅读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;
这将确保执行结束后,无限或过度攻击性循环不会成为执行的最终结果。

我将使用(类似于Jan之前所说的),如果您使用Java 5,至少下面类似的内容可能会告诉您错误是什么(如果有)

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();
}
我会使用(类似于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();
}


您是否曾在控制台中看到
Finished
或任何其他输出?否,它只写入第一个println(“正在执行…”),您从
waitFor()获得的退出值是多少
?是否.exe希望用户对标准输入或无法显示的GUI提供一些反馈?我至少可以看到一个打字错误,这会阻止您的代码编译。请向我们展示您的实际代码,而不是其近似值。您是否曾在控制台中看到
Finished
或任何其他输出?不,它只写入第一个println(“正在执行…”),您从
waitFor()获得的退出值是多少
?是否.exe希望用户对标准输入或无法显示的GUI提供一些反馈?我至少可以看到一个打字错误,这会阻止您的代码编译。请告诉我们你的实际代码,而不是近似代码。事实上,你可能比你想象的更接近答案。许多进程在其输出流被刷新之前不会退出。阅读进程的
InputStream
的内容。对,听起来很有可能!是的,这就是问题所在,如果我阅读它完成的过程的错误流,谢谢。我会更新这个问题,让代码生效。事实上,你可能比你想象的更接近答案。许多进程在其输出流被刷新之前不会退出。阅读进程的
InputStream
的内容。对,听起来很有可能!是的,这就是问题所在,如果我阅读它完成的过程的错误流,谢谢。我将更新该问题以放置有效的代码。
exitValue()
如果在完成之前调用它,将抛出
IllegalThreadStateException
。这就是为什么他要使用
waitFor()
我理解,但是如果在catch块之后应用它,应该会发现进程执行是否结束。好吧,我误解了你的意思。但无论如何,这都不会有帮助,因为他的进程永远不会终止。是的,听起来不错,那么process builder在这里可能会更方便。
exitValue()
如果在完成之前调用它,将抛出一个
IllegalThreadStateException
。这就是为什么他要使用
waitFor()
我理解,但是如果在catch块之后应用它,应该会发现进程执行是否结束。好吧,我误解了你的意思。但无论如何,它都不会有帮助,因为他的进程永远不会终止。是的,听起来不错,也许process builder在这里会更方便。我正在尝试使用process builder,但我得到的错误为2,它说它无法运行程序“pdftops.exe”。我已经用pb.directory(新文件(path_to_directory))设置了目录;你在使用什么作为路径到目录,我感觉你在使用无效的目录…新文件(“C:\\Users\\Joel\\Downloads\\xpdfbin-win-3.03\\bin32\”)奇怪。。。但您可能希望尝试
ProcessBuilder pb=newprocessbuilder(“C:\\Users\\Joel\\Downloads\\xpdfbin-win-3.03\\bin32\\pdftops.exe”、“input.pdf”、“output.ps”),并且根本不使用
pb.directory
命令。它假设设置工作目录,但似乎不适合您。我正在尝试使用ProcessBuilder,但我得到错误=2,它说它无法运行程序“pdftops.exe”。我已经用pb.directory(新文件(path_to_directory))设置了目录;您使用什么作为路径\u到\u目录,