Reprinted to indicate the source:Http://blog.csdn.net/jiangsq12345
I will omit the introduction to javarmi. This article mainly helps you solve a common annoying exception (especially stub not found) that occurs during RMI running ).
Note the following before you start:
1. code writing. I think most people use Eclipse IDE, but it is troublesome to run RMI IDE, therefore, make preparations for compiling and running Java programs with commands first (three command windows are required, namely the server
+ Client
+ RMI Registration Application rmiregistry
)
2. there are many tutorials on the Internet that write a part of the code and compile it before running it. I think writing the Code requires consistency. Therefore, I advocate writing the code first and then running RMI; j. We suggest you start with the simplest example:
Four classes (three on the server and one on the client); Put these three classes under the same package, or if you do not understand the meaning of the package, you simply do not need to use the package by default.
.
Let's take a look at the general steps of RMI:
1. Create a remote interface and declare a remote method (hellointerface. Java)
2. implement remote interfaces and remote methods (inherit unicastremoteobject) (hello. Java)
3. The client searches for remote objects and calls the remote method (helloclient)
4. Start RMI Registration Service and register remote objects (helloserver. Java)
5. Execute the program: start the service helloserver; run the client helloclient to call
Code reference http://topic.csdn.net/u/20090306/22/060e1a70-8d4a-47e9-987c-11637d6d7e7f.html
, I omitted
Let's focus on the compilation and running steps and notes:
Assume that the structure of our code is as follows:
+ Projet
+ SRC
+ Test (Package)
-Hellointerface. Java
-Hello. Java
-Helloserver. Java
-Helloclient. Java
1. Compile the code
Go.../Workspace/Projet/src
Folder (ellipsis indicates the absolute path of your system), first add a default classpath to the environment variable,
In Linux: Export classpath ='.../Workspace/Projet/src
';
In Windows, you can manually right-click my computer and set it manually.
Run the following command ):
$ Javac *. Java
2. Use rmic to generate a stub class
Command (note that the package and class name are used.
Connection without. Class ):
$ Rmic test. Hello
3. Run rmiregistry,
Run the following command in another window:
$ Rmiregistry
4. Run the server (I remember that stub can be directly found without setting the path in earlier versions, but it seems that it is not working now, no matter how to add a path is correct)
To.../Workspace/Projet/src directory
$ Java-djava. RMI. server. codebase = file:/.../workspace/Projet/src/test. helloserver
5. Run the client
To.../Workspace/Projet/src directory
$ Java test. helloclient
If there are no other accidents, You can see helloworld. If you have any questions, leave a message for me.
Note:
1. When using RMI to pass an object, the object must be serialized.
2.
Rmiregistry
The default port is 1099. Therefore, it must be the same as the port used to publish services on the server. Otherwise, the registration cannot be completed. Exception of connection refuse may occur.
3. If the project is large and the code is large, you can use ant
, Example
<Project name = "projet" default = "compile"> </P> <p> <property name = "src" value = "$ {basedir}/src"/> <br/> <property name = "bin" value = "$ {basedir}/bin"/> </P> <p> <path id = "classpath"> <br/> <fileset dir = "$ {bin}" includes = "*. class "/> <br/> </path> </P> <p> <! -- ===============--> <Br/> <! -- Compilation --> <br/> <! -- ===============--> </P> <p> <target name = "compile" Description = "compile le projet"> <br/> <mkdir dir = "$ {bin}"/> <br/> <javac destdir = "$ {bin}" srcdir = "$ {SRC}" classpathref = "classpath "/> <br/> </Target> </P> <p> <! -- =======================================--> <Br/> <! -- Nettoyage des Ré pertoires --> <br/> <! -- =======================================--> </P> <p> <target name =" clean "Description =" Nettoyage des Ré pertoires "> <br/> <Delete dir =" $ {bin} "/> <br/> </Target> </P> <p> </Project>