title: Stopping RMI Server (Java)
body: <p>I created a sucessful RMI server, thank god for that :)</p>
  
  <p>It works perfectly..
  I have a JForm and it gets activated clicking on a button.</p>
  
  <p>Altough i wanted to create too a button to deactivate it, but i am having a problems to unexport it.</p>
  
  <p>Ok then and this is the way i was trying to exporting and terminating the RMI Server</p>
  
  <pre><code>private void btStopServerActionPerformed(java.awt.event.ActionEvent evt) {
   try {
              // Nome do objecto
              String objectname = txtObjectName.getText();
              // obtem o número da porta
              int port = Integer.parseInt(txtServerPort.getText());
  
              RemoteMessageObject remote = new RemoteMessageObject();
              Registry registry = LocateRegistry.getRegistry(port);
              UnicastRemoteObject.unexportObject(LocateRegistry.getRegistry(port), true);
              registry.unbind(objectname);
  
  
              System.out.println("Server offline");
          } catch (IOException ex) {
              Logger.getLogger(ServerGUI.class.getName()).log(Level.SEVERE, null, ex);
          } catch (NotBoundException ex) {
              Logger.getLogger(ServerGUI.class.getName()).log(Level.SEVERE, null, ex);
          }
      }       
  </code></pre>
  
  <p>I get this exception:</p>
  
  <pre><code>java.rmi.NoSuchObjectException: object not exported
  </code></pre>
  
  <p>at the line: </p>
  
  <pre><code>UnicastRemoteObject.unexportObject(LocateRegistry.getRegistry(port), true);
  </code></pre>
  
  <p>What am i doing wrong here?</p>
  
  <p>Solved................</p>
  
  <p>I discovered it before :P
  Anywway thanks @EJP for the correct answer.</p>
  
  <p>So the solution for me was to create the Register when the class starts as a public variable, so it could be use inside both click events (Start server and stop server).</p>
  
  <p>I removed too a lot of nonsense stuff that was not necessary for disabling the RMI server just like @EJP sayed.</p>
  
  <p>Its now working this way:</p>
  
  <pre><code>private void btStopServerActionPerformed(java.awt.event.ActionEvent evt) {                                             
  
      try {
          // Nome do objecto
          String objectname = txtObjectName.getText();
          // obtem o número da porta
          int port = Integer.parseInt(txtServerPort.getText());
  
          Registry registry = LocateRegistry.getRegistry(port);
          UnicastRemoteObject.unexportObject(this.registry, true);
          registry.unbind(objectname);
  
          System.out.println("Server offline");
      } catch (IOException ex) {
          GuiUtils.addText(txtLog, "Erro", "Servidor desligado");
          btStopServer.setEnabled(false);
          btStartServer.setEnabled(true);
      } catch (NotBoundException ex) {
          Logger.getLogger(ServerGUI.class.getName()).log(Level.SEVERE, null, ex);
      }
  }      
  </code></pre>