title: Clean way to stop RMI server
body: <p>A RMI server which works fine without the stopServer functionality.</p>
  
  <pre><code>public class HelloServer extends UnicastRemoteObject implements HelloInterface
  {
      private final static int PORT=1102;
      private final String serverName="server"; 
      private Timer timer;
  
      public HelloServer()  throws RemoteException 
      {
         timer = new Timer();  //At this line a new Thread will be created
         timer.schedule(new StopServerTask(), 5000);
  
      }
  
      @Override
      public String serverResponse(String request) throws RemoteException 
      {
      return "Hello"+request;
      }
  
  
      public static void main(String[] args)
      {
  
          try 
          {
  
              HelloServer skeleton=new HelloServer();
              System.out.println("Starting server");
              skeleton.startServer();
              System.out.println("Server started");
  
  
          } 
          catch (RemoteException ex) 
          {
              ex.printStackTrace();
          }
  
  
      }
  
      public void startServer()
      {
    try {
  
              HelloServer skeleton=new HelloServer();
              Registry reg=LocateRegistry.createRegistry(PORT);
              reg.rebind(serverName, skeleton);
              System.out.println("Server is ready");
  
          } catch (RemoteException ex) 
          {
              Logger.getLogger(HelloInterface.class.getName()).log(Level.SEVERE, null, ex);
          }    
      }
  
      public void stopServer()
      {
      System.out.println("Stopping server");
          try {
  
              Registry rmiRegistry = LocateRegistry.getRegistry(PORT);
              HelloInterface myService = (HelloInterface) rmiRegistry.lookup(serverName);
  
              rmiRegistry.unbind(serverName);
  
              UnicastRemoteObject.unexportObject(rmiRegistry, true);
  
          } catch (NoSuchObjectException e) 
          {
              e.printStackTrace();
          } catch (NotBoundException e) 
          {
              e.printStackTrace();
          } catch (RemoteException ex) {
              Logger.getLogger(HelloServer.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      class StopServerTask extends TimerTask
          {
  
          @Override
          public void run() 
          {
          stopServer();
          }
  
          }
  }
  </code></pre>
  
  <p>Whenever stopServer() in invoked exception is thrown at </p>
  
  <pre><code>UnicastRemoteObject.unexportObject(rmiRegistry, true);
  </code></pre>
  
  <p>Here is the stack Trace</p>
  
  <pre><code>java.rmi.NoSuchObjectException: object not exported
      at sun.rmi.transport.ObjectTable.unexportObject(ObjectTable.java:153)
      at java.rmi.server.UnicastRemoteObject.unexportObject(UnicastRemoteObject.java:297)
      at rmi.HelloServer.stopServer(HelloServer.java:84)
  </code></pre>
  
  <p>Things are same even when I clean the service object by using</p>
  
  <pre><code> UnicastRemoteObject.unexportObject(myService, true);
  </code></pre>
  
  <p>Could someone suggest a clean way to stop the server which also releases the port for reuse.</p>