title: Execute command displaying console window and also get handle of the process
body: <p>I am trying to run a command from Java that will start a process that runs for several minutes. I need to just trigger the command and get the process handle and continue with other operations in a loop. At regular intervals, I will have to monitor that the process is still active. </p>
  
  <p>I also need the console window to display to show the output of the process for the user.</p>
  
  <p>Currently, I have tried methods from both Runtime and ProcessBuilder classes to run my command but neither of them has helped me achieve my objective.</p>
  
  <p>Sample code:</p>
  
  <pre><code>//Changing the directory and running Maven exec: java command on the POM file in that directory.
  
  String cmd = "cd C:/Test &amp; mvn exec:java"; 
  String finalCmd = "cmd /c \""+ cmd +"\"";
  
  Process process = Runtime.getRuntime().exec(finalCmd);
  Thread.sleep(10);
  boolean alive = process.isAlive();
  </code></pre>
  
  <p>The value of variable alive is True, but I don't see the process got started. When the program execution is complete, only then the process starts and I am not sure why that happens. </p>
  
  <p>Also to display the console window, I found from google that I need to use the below command:</p>
  
  <pre><code>String finalCmd = "cmd /c start cmd.exe /c \"" + cmd + "\"";
  </code></pre>
  
  <p>However, with this, the process starts immediately but I do not get the process handle as I find the alive variable shows false.</p>
  
  <p>Does someone know how this objective can be achieved? I am ok if it's not possible to do both at the same time but at least I need to get the process execution to start and get the handle to monitor the process state later in my code.</p>