title: Java RMI, Port already in use when creating registry
body: <p>I'm getting my feet wet in RMI for the first time after working in Java for a number of years, and I'm following <a href="http://www.eg.bucknell.edu/~cs379/DistributedSystems/rmi_tut.html" rel="nofollow noreferrer">this</a> tutorial to get the basics down. The server code below is giving me the following exception saying that port 1099 is already in use, but looking at the output of netstat -ano (on windows), no process is using 1099. I can replace 1099 with any other unused port and it works as expected, with my client program able to connect successfully. </p>
  
  <p>I searched for a while, and <a href="https://stackoverflow.com/questions/8337215/remote-method-invocation-port-in-use">this</a> was the nearest thing I could find to someone having the same problem, but he seems to have found the offending process with netstat, which I cannot.</p>
  
  <p>I'm developing in Netbeans, and I get the same exception whether running through Netbeans or executing the JAR on its own. I'm not starting the registry from the command prompt as he seems to be doing, but that doesn't seem to be necessary, as everything goes just fine if I merely change the port number.</p>
  
  <p>What am I missing here?</p>
  
  <p><strong>Server code:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>import java.rmi.Naming;
  import java.rmi.registry.LocateRegistry;
  
  public class RMIServer {
  
      public static void main(String[] args) throws InterruptedException {
          try {
              LocateRegistry.createRegistry(1099);
              Naming.rebind("//localhost:1099/MyRemote", new MyRemote());
              System.out.println("Server ready.");
          } catch (Exception ex) {
  
              System.out.println("Server error:");
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><strong>Exception:</strong></p>
  
  <pre><code>Server error:
  java.rmi.server.ExportException: Port already in use: 1099; nested exception is: 
      java.net.BindException: Address already in use: JVM_Bind
      at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:328)
      at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:236)
      at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
      at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
      at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:207)
      at sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:122)
      at sun.rmi.registry.RegistryImpl.&lt;init&gt;(RegistryImpl.java:108)
      at java.rmi.registry.LocateRegistry.createRegistry(LocateRegistry.java:203)
      at rmiserver.RMIServer.main(RMIServer.java:10)
  Caused by: java.net.BindException: Address already in use: JVM_Bind
      at java.net.TwoStacksPlainSocketImpl.socketBind(Native Method)
      at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
      at java.net.TwoStacksPlainSocketImpl.bind(TwoStacksPlainSocketImpl.java:101)
      at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:175)
      at java.net.ServerSocket.bind(ServerSocket.java:376)
      at java.net.ServerSocket.&lt;init&gt;(ServerSocket.java:237)
      at java.net.ServerSocket.&lt;init&gt;(ServerSocket.java:128)
      at sun.rmi.transport.proxy.RMIDirectSocketFactory.createServerSocket(RMIDirectSocketFactory.java:45)
      at sun.rmi.transport.proxy.RMIMasterSocketFactory.createServerSocket(RMIMasterSocketFactory.java:349)
      at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:667)
      at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:317)
      ... 8 more
  </code></pre>
  
  <p><strong>Update:</strong>
  Just to be more explicit, here's the complete output of <code>netstat -anp tcp</code> just prior to running the server and getting the exception:</p>
  
  <pre><code>C:\&gt;netstat -anp tcp
  
  Active Connections
  
    Proto  Local Address          Foreign Address        State
    TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
    TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
    TCP    0.0.0.0:2002           0.0.0.0:0              LISTENING
    TCP    0.0.0.0:5800           0.0.0.0:0              LISTENING
    TCP    0.0.0.0:5900           0.0.0.0:0              LISTENING
    TCP    0.0.0.0:16992          0.0.0.0:0              LISTENING
    TCP    0.0.0.0:16993          0.0.0.0:0              LISTENING
    TCP    0.0.0.0:24800          0.0.0.0:0              LISTENING
    TCP    127.0.0.1:1027         127.0.0.1:24801        ESTABLISHED
    TCP    127.0.0.1:1035         127.0.0.1:2002         ESTABLISHED
    TCP    127.0.0.1:1036         0.0.0.0:0              LISTENING
    TCP    127.0.0.1:1045         127.0.0.1:24801        ESTABLISHED
    TCP    127.0.0.1:1092         0.0.0.0:0              LISTENING
    TCP    127.0.0.1:2002         127.0.0.1:1035         ESTABLISHED
    TCP    127.0.0.1:5152         0.0.0.0:0              LISTENING
    TCP    127.0.0.1:24801        0.0.0.0:0              LISTENING
    TCP    127.0.0.1:24801        127.0.0.1:1027         ESTABLISHED
    TCP    127.0.0.1:24801        127.0.0.1:1045         ESTABLISHED
    TCP    192.168.2.76:139       0.0.0.0:0              LISTENING
    TCP    192.168.2.76:1029      64.74.103.175:443      ESTABLISHED
    TCP    192.168.2.76:1038      192.168.2.220:445      ESTABLISHED
    TCP    192.168.2.76:1042      192.168.2.55:445       ESTABLISHED
    TCP    192.168.2.76:1057      74.125.225.40:443      ESTABLISHED
    TCP    192.168.2.76:1058      74.125.133.95:443      TIME_WAIT
    TCP    192.168.2.76:1060      74.125.142.125:5222    ESTABLISHED
    TCP    192.168.2.76:1063      74.125.225.47:443      ESTABLISHED
    TCP    192.168.2.76:1064      74.125.225.34:443      ESTABLISHED
    TCP    192.168.2.76:1066      74.125.225.47:443      ESTABLISHED
    TCP    192.168.2.76:1082      74.125.142.125:5222    ESTABLISHED
    TCP    192.168.2.76:1138      74.125.225.32:443      TIME_WAIT
    TCP    192.168.2.76:1139      74.125.225.32:443      TIME_WAIT
    TCP    192.168.2.76:1140      173.194.46.37:443      ESTABLISHED
    TCP    192.168.2.76:1143      91.199.212.171:80      CLOSE_WAIT
    TCP    192.168.2.76:1144      178.255.82.1:80        CLOSE_WAIT
    TCP    192.168.2.76:1150      74.125.225.32:443      TIME_WAIT
    TCP    192.168.2.76:1152      198.252.206.16:80      ESTABLISHED
    TCP    192.168.2.76:1153      198.252.206.16:80      ESTABLISHED
    TCP    192.168.2.76:24800     192.168.2.52:15713     ESTABLISHED
  </code></pre>
  
  <p><strong>Update:</strong>
  I did some more research just on the idea of a port being in use without appearing in netstat (not RMI specific), but <a href="https://stackoverflow.com/questions/13337759/used-port-not-showing-up-in-netstat-a-result">this</a> was the only thing that seemed similar, and it's not very helpful. Has anyone else encountered this sort of thing before?</p>