How to tell rmiregistry where to look for the classes?

1.4k Views Asked by At

Oracle says that registry is

a bootstrap naming service that is used by RMI servers on the same host to bind remote objects to names

Now, I have such a server that uses rmiregistry for providing JNDI.

public class ObjectProvider {
    public static void main(String[] args) {
        System.setProperty("java.rmi.server.codebase", "file:/absolute/path/to/jar/where/person/class/is/my.jar");
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
        Context context = new InitialContext(env);
        context.bind("jordan", new Person("Michael Jordan"));
        Person p = (Person) context.lookup("jordan");
        System.out.println("jordan = " + p.getName());
    }
}

And Person class:

public class Person implements Remote, Serializable {
    String name;
    public Person(String name) { this.name = name; }
    public String getName() { return name; }
}

And rmiregistryis started as rmiregistry &. Yet, when I run the code it complains about not being able to unmarshall arguments when performing bind because the class Person cannot be found.

I understand that rmiregistry does not find the class file but I don't understand why. Is this the right way to tell it where it can find the classes to be bound?

1

There are 1 best solutions below

7
Little Santi On

Though I cannot spot the problem with your program (I've reproduced it and got the same error), I can suggest you to set an explicit CLASSPATH environment variable in the same process before starting rmiregistry. This classpath must be the same as in the RMI server process. Example:

set CLASSPATH=my_class_directory:my_jar1:my_jar2...
rmiregistry &

I admit this is not a good practice according to Oracle documentation, which tells to drop off the CLASSPATH from rmiregistry. But, since you are getting the code from the localhost, it is better than not getting it working.