1 Problem description
Java Web backend Download a special algorithm compressed ZIP file, because it can not use the Java itself with the decompression method, you must use 7ZIP to decompress. Therefore, the issue of extracting files from the external 7zip exe in the Java Web backend is mentioned in this article.
2 Main Implementation 2.1 defining the Buffer class
Class Streamgobbler extends Thread { InputStream is; String type; Public Streamgobbler (InputStream are, String type) { this.is = is; This.type = type; } public void Run () { try { InputStreamReader ISR = new InputStreamReader (is); BufferedReader br = new BufferedReader (ISR); String Line=null; while (line = Br.readline ())! = null) { System.out.println (type + ">" + line); } } catch (IOException I OE) { ioe.printstacktrace ();}} }
2.2 Execute external EXE process
string[] cmd = { "7za.exe", "x", Zippath, "-O" + outputPath}; Runtime RT = Runtime.getruntime (); Process proc = rt.exec (cmd); Listen for error messages Streamgobbler Errorgobbler = new Streamgobbler (Proc.geterrorstream (), "error"); Monitoring output information Streamgobbler Outputgobbler = new Streamgobbler (Proc.getinputstream (), "output"); Start the Listening input errorgobbler.start (); Outputgobbler.start ();//Make sure the runtime.exec process is completed int exitval = Proc.waitfor (); System.out.println ("Exitvalue:" + exitval);
3 Focus on troubleshooting 3.1 process.waitfor when executing in tomcat, 3.1.1 problem causes
Be sure to read the program's stdout and stderr before calling Process.waitfor (), or it might be because the pipe buffer is not enough, the called system commands are blocked on the standard output and the standard error output. Windows is more prone to this problem because the default value for this buffer is smaller.
It is important to note that the stdout and stderr of the reader are blocking operations, which means that the two-line thread must be read separately, instead of being read in one line thread at a time, or there may be a blocking situation.
[http://www.dongliu.net/post/496142]
3.1.2 Other guesses
1. The main process of the program waits for a certain amount of time, but with little time, the process may not be complete at all. Therefore, you need to call the WAITFOR method for a process that takes a long time to work. This method causes the current thread to wait until the process is interrupted. [http://ccchhhlll1988-163-com.iteye.com/blog/1901497]
2. It may be because a process was started in Tomcat, but there is no permission to kill the process, so it is stuck in this interface
4 other
4.1 Using Java to bring the solution Engross
Java.util.zip
Due to inconsistent algorithm, the decompression prompt: "Invalid CEN header (bad compression method)"
4.2 Downloads 7Zip Solution Engross
Download the sevenzipjbinding compression pack on the SourceForge website. And the download is not a packaged compressed file
However, because the latest decompression algorithm is not supported to cancel.
does 4.3 tomcat have permission to invoke external EXE
4.4 Booting Tomcat failed
The server can start the project normally, but the local computer cannot start. Hint message: "org.apache.catalina.LifecycleException:Failed to start component"
When the local TOMCAT6 test calls the external EXE successful, it is considered in the actual project to test the success of the project, the actual project is the use of TOMCAT7, but also contains the corresponding JDK folder, a certain modification. For example: Modify the folder path to store WebApp, etc., delete some unnecessary files, etc.;
The ultimate reason for this is that the lower version of the JDK was used at startup, and the Java_home was set to a packaged JDK folder.
5 References
1. Usage of waitfor in runtime.getruntime.exec
Programming, sometimes need to wait for the call of the System program to complete the operation, the current thread to do the next step, at this time can be used in class process method waitfor () to implement, it will block the yourselves thread until the end of the calling program run.
2. Correct call system command--set timeout for process.waitfor and other
Therefore, it is necessary to add the timeout control in the code. However, process.waitfor () does not natively support time-out settings,
One method is to use the non-blocking Process.exitvalue () method, and then poll the process state, which consumes the CPU so that the polling interval cannot be set too small, which is not perfect.
The other is another thread called the program, in the main thread to find the time-out, directly call Process.destroy () to terminate the process.
3. When Runtime.exec () won ' t
Describes why the Runtime function cannot be executed properly