|
|
RMI-IIOPHello SampleCopyright © 1999 Sun Microsystems, Inc.
|
This is the classic "Hello World!" application adapted to RMI-IIOP. While similar to the Hello World sample presented in the RMI-IIOP Programmer's Guide this sample illustrates developing an RMI program that can be built and run for either the JRMP or IIOP protocols with the "flip of a switch". The "switch" we refer to is an -iiop switch used with the rmic compiler. You also need to pass a different system/applet environment setting to the java command using the -D option, but the code itself need not be changed. This sample illustrates both application and applet client.
The sample code is found in the $JAVA_HOME/demo/rmi-iiop/hello directory. There you will find the following files:
To provide a complete explanation in the steps that follow, we make the following assumptions:
Set your CLASSPATH environment variable so that the current directory (".") is at the beginning.
Check that your PATH environment variable includes $JAVA_HOME/bin:$JAVA_HOME/jre/bin, where $JAVA_HOME is the directory where the Java SDK is installed.
Note: Don't forget to set up a security policy file. See the following section for instructions.
Create a policy file called $DEMO_HOME/policy containing the following code:
grant {
permission java.security.AllPermission;
};
The RMI section in the Java Tutorial contains a discussion of policy files.
After configuring your environment, as explained above, change directory to $DEMO_HOME/hello.
cp $JAVA_HOME/demo/rmi-iiop/hello/*.java .
javac *.java
cd ..
rmic hello.RemoteHelloServer
rmic -iiop hello.RemoteHelloServer
Remember that $SERVER_ROOT/java is your codebase, and that hello is the package name.
The following assumes that the server is run on a host named aslan.narnia.com, that an http server is running, and that the code base is a java directory under the server's root and that the class files are in a hello directory under java.
rmiregistry
tnameserv
java -Djava.rmi.server.codebase=http://aslan.narnia.com/java/
-Djava.security.policy=policy
-Djava.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory
hello.RemoteHelloServer
java -Djava.rmi.server.codebase=http://aslan.narnia.com/java/
-Djava.security.policy=policy
-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
hello.RemoteHelloServer
java -Djava.rmi.server.codebase=http://aslan.narnia.com/java/
-Djava.security.policy=policy
-Djava.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory
hello.RemoteHelloApplication aslan.narnia.com
java -Djava.rmi.server.codebase=http://aslan.narnia.com/java/
-Djava.security.policy=policy
-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
hello.RemoteHelloApplication aslan.narnia.com
appletviewer http://aslan.narnia.com/jrmp.html
appletviewer http://aslan.narnia.com/iiop.html
There are several steps involved and it is easy to make a mistake. You may build for one protocol and run for another, run the wrong name server for the protocol, or forget to copy class files, for example. Here are a couple of errors (exceptions) you may encounter and what they mean.
| Exception | Probable Cause |
|---|---|
| java.lang.ClassCastException | The stub class file cannot be found in the client class path |
| java.lang.NoClassDefFound | The interface class file cannot be found in the client class path |
| java.rmi.ConnectException | Either the server has not been started or you are running a client for the wrong protocol |