New Easy Tutorial for Java RMI using Eclipse

This is a simple tutorial without using security policy and codebase property

In this RMI Programming tutorial, you will be using Eclipse IDE to learn how to create :

  • Simple Remote Object.
  • Server to instantiate (create ) and bind a remote object.
  • Client to invoke remotely an object

As RMI is a Java to Java only communication protocol, you need to install the Java Development Kit ( JDK ) as well as a programming editor ( IDE ) such as Eclipse. For more details of how to install JDK and Eclipse, Use the following tutorial:

The structure of the files for the projects created using Eclipse throughout this tutorials is shown below:

rmi-steps22

1. Server Side

1 Let’s create a new Java Project using Eclipse ( or NetBeans or other editor you prefer), and call it : RMIServerSide-> Click Finish once done

2 Under the RMIServerSide, project, Select New -> Interfacermi-steps1

3 Set the name for the interface as : AdditionInterface –>Click Finish.
rmi-steps2

4 Copy the following code into the AdditionInterface code.

   import java.rmi.*;

   public interface AdditionInterface extends Remote {
	   public int add(int a,int b) throws RemoteException;
   }

5 Select the project RMIServerSide, Click New -> Class, Set the name for the class as : Addition

rmi-steps3

6 Copy the following code into the Addition class.

import java.rmi.*;
import java.rmi.server.*;

public class Addition extends UnicastRemoteObject
         implements AdditionInterface {

      public Addition () throws RemoteException {   }

      public int add(int a, int b) throws RemoteException {
    	  int result=a+b;
    	  return result;
      }
 }

7 Select the project RMIServerSide, Click New -> Class, Set the name for the class as : AdditionServer

8 Copy the following code in the AdditionServer

import java.rmi.*;
import java.rmi.server.*;   

public class AdditionServer {
	   public static void main (String[] argv) {
		   try {
			   Addition Hello = new Addition();			   		   
			   Naming.rebind("rmi://localhost/ABC", Hello);

			   System.out.println("Addition Server is ready.");
			   }catch (Exception e) {
				   System.out.println("Addition Server failed: " + e);
				}
		   }
}

9 Compile your project through clicking Green Play button.

java programming

No worries if you get the following error, cause the rmiregistry is not running yet

rmi-new-1

10 Open your CMD ( DOS Console ).

rmi-steps6

11 Navigate to the bin folder of your project. The location of your project can be known through clicking: Select the project RMIServerSide,, click : File -> Properties

rmi-steps10

Within the CMD black window, type in the full location of the bin folder as :

cd C:\Users\imed\workspace\RMIServerSide\bin

Make sure you type YOUR location, not mine

Press Enter once done.

12 Run the rmic to generate the stub for the remote object Addition. Run the following command:

rmic Addition -> Press Enter

If you get the message : ‘rmic’ is not recognized…. as shown below. You need to get the path configured to add the bin folder for the JDK

rmi-new-2

Once you have used the rmic compiler, you will find the generated stub in the same folder:
rmi-new-3

13 In the same CMD window start the RMI Registry. Type in directly.

start rmiregistry

14 Now, time to compile and run the AdditionServer ! Click the green button:

java programming

The addition server is ready now !
rmi-new-4

2. Client Side

1 Let’s create a new Java Project using Eclipse ( or NetBeans or other editor you prefer), and call it : RMIClientSide-> Click Finish once done

2 Select the project RMIClientSide, Click New -> Interface, Set the name for the class as : AdditionInterface, Click Finish.

3 Copy the previous code into the AdditionInterface which is :

   import java.rmi.*;

   public interface AdditionInterface extends Remote {
	   public int add(int a,int b) throws RemoteException;
   }

4 Select the project RMIClientSide, Click New -> Class, Set the name for the class as : AdditionClient, Click Finish.

5 Copy the following code into the AdditionClient class

import java.rmi.*;

public class AdditionClient {
	public static void main (String[] args) {
		AdditionInterface hello;
		try {
			hello = (AdditionInterface)Naming.lookup("rmi://localhost/ABC");
			int result=hello.add(9,10);
			System.out.println("Result is :"+result);

			}catch (Exception e) {
				System.out.println("HelloClient exception: " + e);
				}
		}
}

6 Click on the Green Button again to RUN.That’s it for the client side ? you would get the following error !

HelloClient exception: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: Addition_Stub (no security manager: RMI class loader disabled)

This is because there is no stub file on the client side.

7 Visit the bin folder of Server side where you have generated the stub file using the rmic compiler. To browse directly, right click on the server project, click on Show In, and click on System Explorer
rmi-new-6

8 Copy the file (on the usb drive in case you want to run the client on a remote machine).
rmi-new-5

9 Browse to the bin folder of the client and paste the stub file inside the bin folder :

10 Compile and run again ! The result should shown on the console window of Eclipse.

rmi-steps16