Add to Google Reader or Homepage

javax.naming.communicationexception in RMI

When javax.naming.communicationexception will be thrown in RMI?

In java API, it is given that this exception will be thrown when the client is unable to communicate with the naming services.The reason for this exception may be due to server or client side failures.

In this post we will see how server-side failure causes this exception in RMI .

I guess you know about RMI(Remote Method Invocation) through which we can able to access the methods of a remote object. To execute an RMI application we need these components like,





5.RMI registry

Here we are going to focus on the stub, 

what is meant by a stub? 

It is a client-side object used to communicate with the server-side object(skeleton). The client needs to get a stub object.

 From where the client gets its stub object?

The client will get the stub object from the RMI registry, where the server would have already registered  a remote object with a name(RMI url).The client will use the name(RMI url) to get the stub object.

This problem arises when the server is unable to register( or bind) an object to a name (RMI url).

" The reason for this problem is that,when registering a remote object in the RMI registry we need to specify the "path" of the remote interface(in our example - NameCollections) implemented by the object and other components needed by that object, if any of the components needed is not available in the given path then this exception will be thrown."

 See the following example to understand about this exception.



import java.rmi.*;
public interface NameCollections extends Remote
public String getName(int index)throws RemoteException;


import java.rmi.*;
import java.util.*;
import java.rmi.server.*;

class NameStorage extends UnicastRemoteObject implements NameCollections 
private Map name;
public NameStorage()throws RemoteException
name=new HashMap();
public String getName(int index)throws RemoteException
String str=name.get(new Integer(index));
return str;
Prog 3: 

import java.rmi.*;
import javax.naming.*;
class NameServer 
public static void main(String args[])throws RemoteException,NamingException
Context ictxt=new InitialContext();
NameStorage store=new NameStorage();
System.out.println("Clients can invoke the methods....");





Here the NameServer program tries to register a remote object with the RMI url and when registering the object as i have said before we need to specify the path for required files. Here i  have tried to look for files in port:8080,but see what happens.

Have a look at the screen1, there i had executed a command like the following

java -cp . NanoHTTPD 8080 , implies that the server NanoHTTPD has to  serve the files in the port 8080, but if you see the screen 1 it says that the server is serving files from port 80.

I didn't noticed that and i have given the path as codebase= http://localhost:8080/  to look for files that's why i got this error?. 

So i have to change the command as follows codebase= http://localhost:80/ to resolve the above problem. I don't  know is there any problem in NanoHTTPD server or in my laptop.

"But an important thing to be noted here is the path that you provide should exactly point to the location where the necessary files are residing in the server."

In some cases, if you left the trailing slash in the command as given in the below line
 codebase =  http://localhost:8080   then you would get this error.
