code: public static Thread addShutdownHook(final Process process) {
          final Thread thread = new Thread(new Runnable() {
              @Override
              public void run() {
                  if (process != null) {
                      process.destroy();
                      try {
                          process.waitFor();
                      } catch (InterruptedException e) {
                          throw new RuntimeException(e);
                      }
                  }
              }
          });
          thread.setDaemon(true);
          Runtime.getRuntime().addShutdownHook(thread);
          return thread;
      }
docs: Adds a shutdown hook for the process.
  
  @param process the process to add a shutdown hook for
  
  @return the thread set as the shutdown hook
  
  @throws java.lang.SecurityException If a security manager is present and it denies {@link
  java.lang.RuntimePermission <code>RuntimePermission("shutdownHooks")</code>}