Executable jar package calls EXE executable file, child process blocked

Source: Internet
Author: User

Background:

You need to add a button to the project's Test tool to open an EXE tool directly after you click it.

The function of this tool is to import TXT file into Excel report output.

But after parsing two lines will stop moving, also do not error. After you close the test tool, it runs smoothly.

Reason:

TXT to Excel report process, the intermediate information is in memory, the buffer space is occupied, the program is blocked, has been waiting for the release of buffer space resources, so need to establish a thread in time to empty the buffer.

Workaround:

1. Creating the Streamclean thread class

/* * Create threads to clear the blocking area in a timely manner to avoid blocking child threads (problems that occur when calling external tools Txt->excel. ) */public class  Streamclean extends Thread {     InputStream is;     String type;     Public Streamclean (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);//console Output            }        } catch (Ioexce Ption IoE) {             ioe.printstacktrace ();         }     

2. Add the following code at the call exe execution file

      Process process = NULL; try {  process =runtime.getruntime (). exec (Txttoexcel);//txttoexcel is the DOS command that invokes the tool new Streamclean ( Process.getinputstream (), "INFO"). Start ();  New Streamclean (Process.geterrorstream (), "ERROR"). Start (); Process.waitfor ();        } catch (Throwable t) {t.getstacktrace ();      } finally {if (process! = null) {    Process.destroy ();} process = Null;
   }

There's a problem with the wood.


Executable jar package calls EXE executable file, child process blocked

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.