title: java.rmi.ConnectException: Connection refused to host when RMI server is child process of RMI client
body: <p>I have two Java programs - RMIServer and RMIClient. Everything just works as expected, if I launch them as two separate Java invocations. That is</p>
  
  <p><code>$ java -cp someclasspath server.mainserverclass</code> &amp;</p>
  
  <p><code>$ java -cp someclasspath client.mainclientclass</code></p>
  
  <p>But in my project, it so happens that I need the client java program to spawn the server and then connect to it. I used both the <code>runtime.exec()</code> method and ProcessBuilder to spawn a server instance, but when I do that I get the following:</p>
  
  <pre><code>.Exception in thread "main" java.rmi.ConnectException:  
      Connection refused to host: 0:0:0:0:0:0:0:1; nested exception is:
  </code></pre>
  
  <p>But the interesting part is that if manually kill the RMIClient instance, the RMIServer gets orphaned(parent is <code>pid:1</code>), and then if I run the RMIClient, I can connect to orphaned RMI server just fine!</p>
  
  <p>So, in seems like when the RMIServer and RMIClient have a parent-child relationship, I am unable to connect via RMI.</p>
  
  <p>Any help/pointers is much appreciated.</p>
  
  <p>*****ANSWER***** (Apparently I do not have enough reputation to answer my own question :-)</p>
  
  <p>This was my mistake. The RMIServer and RMIClient classes were instantiated in two separate Java  threads. The RMIClient class for instantiated <em>before</em> the RMIServer registered the remote object and hence I get this exception.</p>
  
  <p>The parent child relationship is irrelevant to this problem. It so happened that order in which these classes were getting created was the root cause of the problem.</p>