title: Restart Tomcat with Java
body: <p>I need to restart the tomcat from Java code. </p>
  
  <p>For eg, if a query is not executed for a certain time Period then it will restart the tomcat automatically.</p>
  
  <p>I have tried the following shutdown and startup code, but when we shutdown the tomcat then the java code will not run and tomcat not started.</p>
  
  <p><strong>Note :- I am running this code from a application and restarting the same tomact which the same application is using.</strong></p>
  
  <p>Following the code</p>
  
  <pre><code>try {
  
      PreparedStatement.setQueryTimeout(10);
      rs = PreparedStatement.executeQuery();
  
  } catch (SQLException ex) {
  
     System.out.println("IN CATCH BLOCK FOR THE REFRESH INVOICE");
  
     String shutcommand = "killall java";
      Process shutchild = Runtime.getRuntime().exec(shutcommand);
  
      System.out.println("JAVA PROCESS KILLED");
  
      String locationCommand = "cd /root/cluster/tomcat6/bin";
      Process locationChild = Runtime.getRuntime().exec(locationCommand);
  
      String strtcommand = "./startup.sh";
      Process strtchild = Runtime.getRuntime().exec(strtcommand);
  }
  </code></pre>