title: Exit hook for Java process
body: <p>I am creating a TFTP server and keep it running using a infinite loop. When i try to send a kill signal </p>
  
  <pre><code>/bin/kill -s SIGINT &lt;pid&gt;
  </code></pre>
  
  <p>I want Java process to close TFTP server and exit.</p>
  
  <pre><code>class TFTP {
  public void startTFTP(String str, String str1){
      try {
          //Start TFTP server
              // Both of below are not working.
              Runtime.getRuntime().addShutdownHook(new Thread(){
                  @Override
                  public void run(){
                      //Shutdown tftp server
                      System.out.println("Exiting");
                      System.exit(0);
                  }
              });
  
              SignalHandler handler = new SignalHandler () {
                  public void handle(Signal signal) {
                      System.exit(0);
                  }
              };
              Signal.handle(new Signal("INT"), handler);
              Signal.handle(new Signal("TERM"), handler);
  
              while(true){
              // Infinite loop to keep process running.
              }
          }
      } catch (Exception e) {
  
      }
  }
  public static void main(String args[]){
          TFTP tftp = new TFTP();
          tftp.startTFTP(args[0],args[1]);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I kill the process from a script and calling</p>
  
  <pre><code>/bin/kill -s SIGINT &lt;pid&gt;
  </code></pre>
  
  <p>doesn't kill the java process for some reason. I thought SIGINT is handled as part of </p>
  
  <pre><code>Signal.handle(new Signal("INT"), handler);
  </code></pre>
  
  <p>What am i missing here?</p>
  
  <p>Regards<br>
  Dheeraj Joshi</p>