I found a simple RMI example on the Internet. I didn't expect it to be a simple example. I encountered many problems. I will analyze these problems and solutions for your reference.
Post Code first
Remote interface helloin. Java
Import java. RMI .*;
Public interface helloin extends java. RMI. Remote {
String sayhello () throws RemoteException;
}
Hello. Java
Import java. RMI .*;
Import java.net .*;
Import java. RMI. Registry .*;
Import java. RMI. server .*;
Public class Hello extends java. RMI. server. unicastremoteobject implements helloin {
Public Hello () throws RemoteException {
Super ();
}
Public String sayhello () throws RemoteException {
Return "Hello, world! ";
}
Public static void main (string [] ARGs) {// Service Registration
Try {
Hello H = new Hello ();
Java. RMI. Naming. rebind ("hello", H );
System. Out. Print ("ready ......");
}
Catch (exception e ){
E. printstacktrace ();
}
}
}
Client Program helloworld. Java
Import java. RMI .*;
Import java. RMI. Registry .*;
Public class helloworld {
Public static void main (string [] ARGs ){
Try {
Helloin HI = (helloin) Naming. Lookup ("hello ");
For (INT I = 0; I <10; I ++ ){
System. Out. println (Hi. sayhello ());
}
}
Catch (exception e ){
E. printstacktrace ();
}
}
}
OK. The three code files have been written, and then perform the following steps:
1. Switch to the directory where the three code files are located on the terminal
1.1 compile and generate helloin. Class
1.2 compile and generate hello. Class (to be converted into helloin. class, because there is dependency (compiling command javac-CP xxxxxx hello. Java) xxxxxx is helloin. ClassDirectory)
2. rmic Hello generates hello_stub.class (One method is to switch to JDK/bin and run rmic directly, and the other method is to add the hello. Class file directory to classpath)
3. Run hello. Java in eclipse
4. Run rmiregistry (switch to JDK/bin)
5. Run helloworld. Java again.
Some documents mentioned that the skeleton. Class file will be generated, which should also be in theory. However, I did not generate this file according to the steps they mentioned, but the running results are also normal. Which of the following can be explained?
The above method has integrated the error solution and the code has been run.