Search code examples
javaprocessbuilder

How to disable console popup and know when a process created by ProcessBuilder has finished so that it can be destroyed


I am creating java process using ProcessBuilder for ghostscript to convert pdf into tiff as below

process = new ProcessBuilder("D:\\ghost-script\\gs\\gs9.02\\bin\\gswin64.exe","-q",  "-dNOPAUSE", "-dBATCH", "-dMaxStripSize=8192", "-sDEVICE=tiffg4", "-r300x300", "-dDITHERPPI=200", "-sOutputFile=D:\\ghost-script\\example\\output.tif", "D:\\ghost-script\\example\\input2.pdf", "-c", "quit").start();

Now I have two problems.

  1. This process open console popup which I don't need as it has to run on server. What parameter I can set to disable that console popup.
  2. Input file can of any be of any size. How can I know when the process has completed so that I can destroy it. I don't want Thread.sleep(time-to_sleep). Because for different size of input file it is different and if I take upper limit, it will hamper performance for lower size files badly. So what can I do to destroy the process as soon as it get completed?

Any help is much appreciated.


Solution

  • For your first question, the window comes from the executable you launch, you may want to use

    gswin64c.exe
    

    See this topic : Ghostscript suppress output windows when called by command line

    For your second question, use waitFor() on the Process object.

    process.waitFor();