title: Launch Firefox and Wait until it is Closed
body: <h2>Question</h2>
  
  <p>I want to start the Firefox web browser as process to visit a specific website, then wait until it is closed.</p>
  
  <p>A special situation is that the browser may already be open and running, as the user may have visited some website already.</p>
  
  <p>In that case, the browser would probably open a new tab in an existing window and the newly launched process will be terminated immediately. This should not confuse my waiting process: Either, I want a new browser window (if that can somehow be enforced, maybe via command line arguments) and wait until that is closed, or keep the existing browser window and wait until all the tabs resulting from my process are closed.</p>
  
  <h2>Environment</h2>
  
  <p>I think it doesn't matter, but my programming environment is <code>Java</code> and you can assume that I know the path of the browser.</p>
  
  <h2>Example</h2>
  
  <p>The only browser for which I can obtain the expected behavior is <a href="https://stackoverflow.com/questions/3349922">Internet Explorer</a> (sigh.). Here, I need to basically create a new batch script in a temp folder with something like</p>
  
  <pre><code>start /WAIT "" "C:\Program Files\Internet Explorer\iexplore.exe" -noframemerging http://www.test.com/
  </code></pre>
  
  <p>I then run the batch script instead of directly the browser and delete it once I am finished with waiting.</p>
  
  <h2>Intended Process</h2>
  
  <p>To make the intended process clearer:</p>
  
  <ol>
  <li>My program starts.</li>
  <li>My program launches the Firefox browser as separate process and provides an URL to visit as argument to that process.</li>
  <li>The Firefox browser runs asynchronously, as a new process, and visits the provided URL. So far, this is easy.</li>
  <li>After launching the new process (the Firefox browser), my own program should wait for the said process to terminate. This is the hard part, as
  
  <ol>
  <li>Many modern browsers start multiple processes. I would need to wait for all of them.</li>
  <li>Many modern browsers may somehow "detach" themselves from the process that I launched. Sorry, I don't know a better word, what I mean is: I start a process which then starts another process and terminates immediately while the other process keeps running. If I wait for the browser process originally started by my program, the waiting will be finished while the browser is still open.</li>
  <li>A special case of the above is tabbed browsing as realized in many browsers: If the browser is already open (in a separate process started by the user) when I launch it, my newly started browser process may simple communicate the URL to visit to the existing process and terminate. The user is still on my provided URL while my program thinks she has closed the browser. This issue can maybe be forbidden by specifying a special command line argument, like <code>noframemerging</code> for the IE.</li>
  </ol></li>
  <li>Once the browser has terminated or all tabs related to the URL I provide have been closed, my program will cease to wait and instead continue doing its business.</li>
  </ol>
  
  <p>The use case is that I have a web application which can either run locally or on a server. If it is run locally, it launches a web server, then opens the browser to visit the entry page. Once the browser is closed, that web application should shut down as well. This works reliable for Internet Explorer, for all other cases, the user has to close the browser and then, explicitly, the web application. Thus, if I could wait reliably for Firefox to finish, this would make the user experience much better.</p>
  
  <h2>Solution Preferences:</h2>
  
  <p>Solutions are prefered in the following order</p>
  
  <ol>
  <li>Anything which ships with the pure Java JRE. This includes special command line arguments to the browser.</li>
  <li>Things that require me to, e.g., create a batch script (such as in the IE case.)</li>
  <li>Anything that requires 3rd party open source libraries.</li>
  <li>Anything that requires 3rth party closed source libraries.</li>
  </ol>
  
  <p>Any platform independent answer (working both Windows and Linux) is prefered over platform-dependent ones.</p>
  
  <p>Reason: In the ideal case, I would like to know what exactly is done and include it into my own code. As I want to support different browsers (see "PS" below), I would like to avoid having to include one library per browser. Finally, I cannot use commercial or closed source libraries, but if no better answer turns up, of course, I will honor any working solution with an accept. I will accept the first (reasonably nice) working answer of type "1". If answers of lower preference turn up, I will wait a few days before accepting the best one among them.</p>
  
  <h2>PS</h2>
  
  <p>I will launch a couple of similar questions for other browsers. Since I believe that browsers are quite different in the command line arguments they digest, the way the launch threads and sub-processes, I think this makes sense.</p>
  
  <ul>
  <li>Similar question regarding Chrome: <a href="https://stackoverflow.com/questions/31911440">Launch Chrome and Wait Until it is Closed</a></li>
  <li>Similar question regarding Opera: <a href="https://stackoverflow.com/questions/31916896">Launch Opera and Wait Until it is Closed</a></li>
  <li>Similar question regarding Chromium: <a href="https://stackoverflow.com/questions/31919123">Launch Chromium and Wait Until it is Closed</a></li>
  <li>Similar question regarding Edge: <a href="https://stackoverflow.com/questions/31921095">Launch Edge Browser and Wait Until it is Closed</a></li>
  <li>Similar question regarding Safari: <a href="https://stackoverflow.com/questions/31928891">Launch Safari and Wait Until it is Closed</a></li>
  </ul>