Check a lot of information, almost started to turn Java source code, and finally combined with an article (forget the source), thought of the output stream will block process execution. The Java process executes an input stream with two output streams (relative to an external program). When two output streams have content output, and the Java execution program does not empty the output stream in time, it blocks the process.
Now posted code, hoping to help the need to peer:
Copy Code code as follows:
/**
* PDF Transfer SWF function
* @param path input and output file paths
* @param inputfilename Input file name
* @param outputfilename Output file name
* @return file-generated SWF file
*/
private static File toswf (String sourcefile, String destfile, String command) {
Long begintime = System.nanotime ();
Runtime RT = Runtime.getruntime ();
try {
Process process = rt.exec (command);
Final InputStream isnormal = Process.getinputstream ();
New Thread (New Runnable () {
public void Run () {
BufferedReader br = new BufferedReader (new InputStreamReader (Isnormal));
StringBuilder buf = new StringBuilder ();
String line = null;
try {
while (line = Br.readline ())!= null) {
Buf.append (line + "\ n");
}
catch (IOException e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("The output result is:" + buf);
}
). Start (); Start a separate thread to empty the buffer of Process.getinputstream ()
InputStream isError = Process.geterrorstream ();
BufferedReader Br2 = new BufferedReader (new InputStreamReader (IsError));
StringBuilder buf = new StringBuilder ();
String line = null;
while (line = Br2.readline ())!= null) {
Buf.append (line + "\ n");
}
SYSTEM.OUT.PRINTLN ("Error output is:" + buf);
try {
Process.waitfor ();
catch (Interruptedexception e) {
E.printstacktrace ();
}
catch (IOException e) {
E.printstacktrace ();
}
Long endtime = System.nanotime ();
SYSTEM.OUT.PRINTLN ("Transfer swf time consuming:" + (Endtime-begintime)/1000000000 + "seconds" + sourcefile);
return new File (destfile);
}