问题
I have two Java programs - RMIServer and RMIClient. Everything just works as expected, if I launch them as two separate Java invocations. That is
$ java -cp someclasspath server.mainserverclass
&
$ java -cp someclasspath client.mainclientclass
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 runtime.exec()
method and ProcessBuilder to spawn a server instance, but when I do that I get the following:
.Exception in thread "main" java.rmi.ConnectException:
Connection refused to host: 0:0:0:0:0:0:0:1; nested exception is:
But the interesting part is that if manually kill the RMIClient instance, the RMIServer gets orphaned(parent is pid:1
), and then if I run the RMIClient, I can connect to orphaned RMI server just fine!
So, in seems like when the RMIServer and RMIClient have a parent-child relationship, I am unable to connect via RMI.
Any help/pointers is much appreciated.
*****ANSWER***** (Apparently I do not have enough reputation to answer my own question :-)
This was my mistake. The RMIServer and RMIClient classes were instantiated in two separate Java threads. The RMIClient class for instantiated before the RMIServer registered the remote object and hence I get this exception.
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.
回答1:
it so happens that I need the client java program to spawn the server and then connect to it
Why? In that circumstance you don't even need RMI let alone the second JVM. Just create the remote object in the current JVM and call its methods directly.
RMI itself is completely unaware of the parent/child relationship so I wouldn't pursue that line of investigation. The exception string 'Connection refused to host: 0:0:0:0:0:0:0:1' is extremely suspicious. Where did you get the stub for the remote object? That's where the connect target comes from.
来源:https://stackoverflow.com/questions/7898120/java-rmi-connectexception-connection-refused-to-host-when-rmi-server-is-child-p