You are on page 1of 16

P.O.

Box85 KARONGI- RWANDA

IPRC KARONGI Tel: +250 788871075

Email:info@iprckarongi.rp.ac.rw
Integrated Polytechnic Regional College
www.iprckarongi.rp.ac.rw

INFORMATION AND COMMUNICATION TECHNOLOGY DEPARTMENT


PROGRAM: INFORMATION TECHNOLOGY

Module Name: Object Oriented Programming Using Java


Module Code: ICT212
Module Leader: BIZIMANA Zephanie & IRAGENA Jean d’ Amour Wilson

ADMINISTRATIVE DEPARTMENT: ICT

Year 2, Semester II
Academic year 2019/2020

28 April, 2020

1
Running RMI Program in NetBeans IDE .................................................................................................... 3
Introduction ........................................................................................................................................ 3
1.Create the remote interface.......................................................................................................... 3
2.Create the Implementation Class (Remote Object) ........................................................................ 3
3.Create, Define and Run Server application .................................................................................... 3
4.Create, Define and Run Client application ..................................................................................... 3
Implementation................................................................................................................................... 4
Step 1: Create A new Project Named rmi ......................................................................................... 4
Step 2: Create Java Interface............................................................................................................ 6
Press Right click on rmi, Select New and then Select Java Interface .............................................. 6
Codes........................................................................................................................................... 8
Step 3: Create Java Class: server ...................................................................................................... 9
Press Right click on rmi, Select New and then Select Java Class .................................................... 9
Codes......................................................................................................................................... 11
Step 4: Create Java Class: client...................................................................................................... 12
Codes......................................................................................................................................... 13
Testing .............................................................................................................................................. 14
Running the Server Program First................................................................................................... 14
Running the Client Program ........................................................................................................... 15
Note: ............................................................................................................................................. 15
Example: You tried to run Client Program without running the server program First, you will get
Error .......................................................................................................................................... 16

2
Running RMI Program in NetBeans IDE

Introduction

1.Create the remote interface

Extend the Remote interface and declare the RemoteException


Import java.rmi.*;
Public interface Adder extends Remote{
Public int add(int a, int y ) throws RemoteException;
}
Here, interface name is Adder and method name is add()

2.Create the Implementation Class (Remote Object)

Can write an implementation class separately or can directly make the server program implement
this interface.

3.Create, Define and Run Server application

 Create a server class from where to invoke the remote Object.


 Create a remote object.
 Get the RMI registry using the getRegistry() method of the LocateRegistry class
which belongs to the package java.rmi.registry.
 Bind the remote object created to the registry using the Rebind() method of the class
named Registry.

4.Create, Define and Run Client application

 Create a client class


 Get the RMI registry using the getRegistry() method of the LocateRegistry class
which belongs to the package java.rmi.registry.
 Fetch the object from the registry using the method lookup() of the class Registry
which belongs to the package java.rmi.registry.
 Invoke the required method using the obtained remote object.

3
Implementation

Step 1: Create A new Project Named rmi

Click Next

4
Click Finish

5
Step 2: Create Java Interface

Press Right click on rmi, Select New and then Select Java Interface

6
Click Finish
Then project Name is: adder.java

7
Codes

package rmi;

import java.rmi.*;

public interface adder extends Remote {


public int add(int n1,int n2) throws RemoteException;

8
Step 3: Create Java Class: server

Press Right click on rmi, Select New and then Select Java Class

9
Click Finish
Then project Name is: server.java

10
Codes

package rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class server extends UnicastRemoteObject implements adder{


public server() throws RemoteException{
super();
}

@Override
public int add(int n1, int n2) throws RemoteException {
return n1+n2;
}
public static void main(String args[]){
try{
Registry reg=LocateRegistry.createRegistry(9999);
reg.rebind("hi Server", new server());
System.out.println("Server is Ready") ;
}
catch(RemoteException e){
System.out.println(e) ;
}

}
}

11
Step 4: Create Java Class: client

Click Finish
Then project Name is: client.java

12
Codes

package rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class client {


public static void main(String args[]) throws RemoteException,
NotBoundException{
client c=new client();
c.connectRemote();

private void connectRemote() throws RemoteException,


NotBoundException{
try{
Scanner sc=new Scanner(System.in);
Registry reg=LocateRegistry.getRegistry("localhost",9999);
adder ad=(adder)reg.lookup("hi Server");
System.out.println("hello Server............");
System.out.println("Enter Two Numbers:");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("Addition is:"+ad.add(a, b));
}catch(NotBoundException | RemoteException e){
System.out.println(e);
}

13
Testing

Running the Server Program First

14
Running the Client Program

Note:
 If the server program is not running the client program also not Running
 Run the Server program First if the Server Program is ready then run the Client
Program

15
Example: You tried to run Client Program without running the server program First, you
will get Error

16

You might also like