Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

EJB Tutorial

New Easy Tutorial for Java RMI using Eclipse


Posted by Imed Bouchrikaon November 12, 2016in Java RMI, ProgrammingNo Comments
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 :

o Simple Remote Object.


o Server to instantiate (create ) and bind a remote object.
o 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:

Java Programming : Creating a simple java project using Eclipse

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

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 -> Interface

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

4 Copy the following code into the AdditionInterface code.


1. import java.rmi.*;
2.  
3. public interface AdditionInterface extends Remote {
4. public int add(int a,int b) throws RemoteException;
5. }
5 Select the project RMIServerSide, Click New -> Class, Set the name for the class as : Addition

6 Copy the following code into the Addition class.


1. import java.rmi.*;
2. import java.rmi.server.*;
3.  
4. public class Addition extends UnicastRemoteObject
5. implements AdditionInterface {
6.  
7. public Addition () throws RemoteException { }
8.  
9. public int add(int a, int b) throws RemoteException {
10. int result=a+b;
11. return result;
12. }
13. }
7 Select the project RMIServerSide, Click New -> Class, Set the name for the class as : AdditionServer
8 Copy the following code in the AdditionServer
1. import java.rmi.*;
2. import java.rmi.server.*;
3.  
4. public class AdditionServer {
5. public static void main (String[] argv) {
6. try {
7. Addition Hello = new Addition();
8. Naming.rebind("rmi://localhost/ABC", Hello);
9.  
10. System.out.println("Addition Server is ready.");
11. }catch (Exception e) {
12. System.out.println("Addition Server failed: " + e);
13. }
14. }
15. }
9 Compile your project through clicking Green Play button.

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

10 Open your CMD ( DOS Console ).


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

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

Once you have used the rmic compiler, you will find the generated stub in the same folder:
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:

The addition server is ready now !

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 :
1. import java.rmi.*;
2.  
3. public interface AdditionInterface extends Remote {
4. public int add(int a,int b) throws RemoteException;
5. }
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
1. import java.rmi.*;
2.  
3. public class AdditionClient {
4. public static void main (String[] args) {
5. AdditionInterface hello;
6. try {
7. hello = (AdditionInterface)Naming.lookup("rmi://localhost/ABC");
8. int result=hello.add(9,10);
9. System.out.println("Result is :"+result);
10.  
11. }catch (Exception e) {
12. System.out.println("HelloClient exception: " + e);
13. }
14. }
15. }
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

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

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.

You might also like