RMI

If you're planning to use OpenSolaris as an RMI server, this little tip could save you some time. As a simple test I was following the Getting Started Using Java RMI tutorial. However, when I attempted to connect to the OpenSolaris server from the client, I was getting the following exception:

Client exception: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
        java.net.ConnectException: Connection refused: connect

Why was RMI trying to connect to 127.0.0.1? I wasn't even using a hostname, I was directly connecting with my server's IP address (10.0.1.10). After doing a bit of debugging, it turns out I was successfully connecting to the RMI registy and getting the stub, however, the IP address placed in the remote stub was 127.0.0.1. What's up with that?

After a bit more research it turns out the RMI registry is using InetAddress.getLocalHost() to determine the IP address to put in the stub. This simple program shows what is returned by that method call:

import java.net.InetAddress;

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("getLocatHost: " + InetAddress.getLocalHost());
    }
}

And Here's the output:

getLocatHost: opensolaris/127.0.0.1 

Looking at the source code for InetAddress.getLocalHost(), a private method getAddressFromNameService() is called. The /etc/nsswitch.conf file is used by OpenSolaris for configuring the address name service. Looking at my setting for hosts:

hosts: files dns # Added by DHCP

So the name service's first place to look is files, and the file that is being looked at is /etc/hosts, which contains the following for my hostname 'opensolaris':

127.0.0.1	opensolaris opensolaris.local localhost loghost

So one solution to this problem is to add my IP address to the /etc/hosts file as follows:

127.0.0.1	opensolaris opensolaris.local localhost loghost
10.0.1.10	opensolaris

Now running my test class above returns:

getLocatHost: opensolaris/10.0.1.10

Which will also allow my RMI client to successfully connect.

If you don't want to mess with your /etc/hosts file, another option is to tell the RMI server which hostname to use by using the java.rmi.server.hostname JVM property. My complete command to start the server now looks as follows (note, I'm running the script from my build directory):

java -classpath `pwd` -Djava.rmi.server.codebase=file:`pwd`/ -Djava.rmi.server.hostname=10.0.1.10 example.hello.Server

Overall, it's upsetting that the RMI Server isn't initially getting the proper IP address, but at least now I understand why.

Comments:

Thanks for a very helpful post. Just a note to say that, if you don't want to put a numeric IP address into hosts, you could try mapping "opensolaris" (in your example) to the fully qualified DNS address of the server.

So in your example the new HOSTS entry would be:
opensolaris.roomfullofdevs.bigandhighlyethicalcorporation.com opensolaris

At least, that worked for me. Thanks again, Joe

Posted by Joe on April 08, 2010 at 09:40 PM GMT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

The Observatory is a blog for users of Oracle Solaris. Tune in here for tips, tricks and more as we explore the Solaris operating system from Oracle.

Connect with Oracle Solaris:


Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today