News, tips, partners, and perspectives for the Oracle Solaris operating system


Guest Author

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:; nested exception is: 
java.net.ConnectException: Connection refused: connect

Why was RMI trying to connect to I wasn't even using a hostname, I was directly connecting with my server's IP address ( 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 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/ 

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':

opensolaris opensolaris.local localhost loghost

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

opensolaris opensolaris.local localhost loghost


Now running my test class above returns:

getLocatHost: opensolaris/

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= 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.

Join the discussion

Comments ( 1 )
  • Joe Thursday, April 8, 2010

    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

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.