Previous lesson 5/6 |home| Next lesson RMI level 2

Remote Method Invocation (Level I)

Compilation and Execution

Till now, you have created 4 classes:

  1. Payment.java-- a remote interface
  2. PaymentImpl.java-- a remote object class
  3. Server.java-- an RMI server
  4. Client.java-- an RMI client

Make sure that you put these four files in one subdirectory. In this case, we put in C:\myrmi\ subdirectory.

 
C:\myrmi\Payment.java
	 PaymentImpl.java
         Server.java
	 Client.java

Compile them using the following commands:

 C:\   Command Prompt
 

C:\myrmi\javac -d . Payment.java PaymentImpl.java Server.java Client.java

Note that the switch "-d ." means "put the compiled files into the current directory". If you want to put the compiled files to some specific directory for example, c:\myrmi\mortgage, you will use command instead:

javac -d mortgage Payment.java .... etc.

Now, you can try the client\server system on your own computer. You may be wondering how it could be to use one computer to try a client\server system. Remember that, you can start different command-line windows to try it. Because every command-line window will get one distinguished Java Virtual Machine(JVM) instance, and the communication between two JVMs can be established in one computer. We will use localhost as a host for the server.

Betty checked the RMI specification, found that a new feature for running an RMI server is available.

New feature: you don't need to generate a stub class before you start the server. The stub class will be generated on the fly if you use jdk 1.5.0 version or above.

If you use jdk version below 1.5.0, you cannot run the examples listed here. You must use rmic utility to generate a stub class. If so, use the following command to generate a stub class:

set classpath=
rmic -v1.2 Server

A file with name Server_stub.class is generated for JVM communication. Now you can go to the next step to start rmiregistry and server.

You need to follow the steps below to run the server:

  1. Start rmiregistry utility
  2. Start a server

Let's start rmiregistry. First make sure the classpath is empty, then use command as follows:

 C:\   Command Prompt
 

C:\myrmi\set classpath=
C:\myrmi\start rmiregistry

Note that an empty command-line window will pop-up. Don't close it, minimize it if you want. That window is for rmiregistry.exe program.

 C:\   Command Prompt
 



Then, you can start a server window in the following command:

 C:\   Command Prompt
 

C:\myrmi\start java Server

The pop-up window will have the following printout which indicates the server is started successfully and ready to accept client connection.

 C:\   Command Prompt
 

Mortgage server is ready to listen...

Note that the RMI server we designed is not for one client connection. It can accept many-client connections simultaneously.

Start a client

When the server starts, you are ready to start a client. Please issue the following command:

 C:\   Command Prompt
 

C:\myrmi\java Client

When the client starts, the LocateRegistry.getRegistry("localhost") sends information to the RMI registry which is held by the localhost(the Server command-line window) and asks to look for a remote object named "Mortgage". Since the server has such remote object, so the server sends an instance of stub class back, or we can say the client downloads the stub class from the localhost server. When the client gets the reference to the remote object, it can use such reference to call remote method calculatePayment() just like to call a local method. This is the most beautiful thing we mentioned earlier for RMI technology.

The following shows all commands and printout when you start the RMI server and client system.

 C:\   Command Prompt
 

C:\myrmi>javac -d . Payment.java PaymentImpl.java Server.java Client.java

C:\myrmi>set classpath=

C:\myrmi>start rmiregistry

C:\myrmi>start java Server

C:\myrmi>java Client
Usage: java Client principal annualInterest years

For example: java Client 80000 .065 15

You will get the output like the following:

The principal is $80000
The annual interest rate is 6.5%
The term is 15 years
Your monthly payment is $696.89

C:\myrmi>java Client 150000 .060 15
The principal is $150000
The annual interest rate is 6.0%
The term is 15 years
Your monthly payment is $1265.79

C:\myrmi>

Congratulations! You have successfully designed a simple RMI server\client system.

It is time for you to recap the process of designing an RMI server and client system:

  1. Design a remote interface.
  2. Design a remote object.
  3. Design an RMI server.
  4. Design an RMI client.

Betty felt so happy. She said to her colleague.
"It's great to see something actually work. I am going to use RMI server to publish the mortgage calculator."

Betty still has some doubts in her mind, like how to get the remote object run at a specific TCP port, why the server is running all the time if no client to connect it, it seems wasting resource? All these issues will be discussed in RMI level 2 tutorial.

Note: if you follow the examples exactly, you will get same result. If you use different subdirectory, or create your own package, the commands used to run server and client may be different. You are recommended to use exact code and commands to try the sample code if you are brand new to the RMI technology. If you did so, but failed to run, please send your information to javacamp.org. Javacamp.org will try its best to help you run the sample programs successfully.

Any comments for the RMI (level I) tutorial? please feel free to fill this form

Check your skill

Compile and run the client/server system you created in the previous sections.

A possible solution:

 C:\   Command Prompt
 

C:\myrmi>javac -d . Hello.java HelloImpl.java HelloServer.java HelloClient.java

C:\myrmi>set classpath=

C:\myrmi>start rmiregistry

C:\myrmi>start java HelloServer

C:\myrmi>java HelloClient
Got info from server: Hello, Betty

C:\myrmi>

Note that: you will have two more command-line windows: One is for rmiregistry which has nothing to print out. The other window is for HelloServer. The message from the server printed out like the following.

 C:\   Command Prompt
 
Hello Server is ready to listen...

Note: you have to start HelloServer first before you start the HelloClient.

If you use Java 5 and got exception, please make a batch file to run all commands at one time. Copy the following code to a notepad and save it as run.bat.

javac -d . Hello.java HelloImpl.java HelloServer.java HelloClient.java

set classpath=

start rmiregistry

start java HelloServer

java HelloClient

At command line, type c:\run

page 1  page 2  page 3  page 4  page 5  page 6  Previous lesson 5/6 |home| Next lesson RMI level 2