I am trying to execute a process from my Java application, when I execute this process from the console it works right, but when I do getRuntime().exec() it starts but never ends, no exceptions, no exit values.
The process I am trying to execute is pdftops.exe, an app that converts PDF files to PostScript.
When I try to convert small files (executing from Java) it works OK, the problem is converting larger PDFs which may take longer (from 20 to 60 seconds). I think the problem may be that the execution time is too long.
Here is the piece of code that calls the program (the command line is simplified, the input.pdf and output.ps are placed in a folder inside my home directory, and pdftops.exe is placed in Desktop):
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();
}
EDIT: Reading the process’ ErrorStream solves the problem:
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();
}
Not immediate answer to your question, but might be useful to capture the error/output stream of the process so you know what is going on there (assuming it produces something).
With java 7 you can use pretty handy ProcessBuilder and merge your error stream into the output one …
Could it be waiting for some input for example?