How to Create Simple RMI Application using Java

Create Simple RMI Application using Java

In this example, We will show you, How to Create Simple RMI Application using Java. The example program were tested and shared in this post.

Arithmatic.java

package com.dineshkrish.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Arithmatic extends Remote {

	public int add(int a, int b) throws RemoteException;
	public int subtract(int a, int b) throws RemoteException;
	public int multiply(int a, int b) throws RemoteException;
	public int divide(int a, int b) throws RemoteException;
}

ArithmaticImpl.java

package com.dineshkrish.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ArithmaticImpl extends UnicastRemoteObject implements Arithmatic {

	protected ArithmaticImpl() throws RemoteException {

	}

	@Override
	public int add(int a, int b) throws RemoteException {

		return a + b;
	}

	@Override
	public int subtract(int a, int b) throws RemoteException {

		return a - b;
	}

	@Override
	public int multiply(int a, int b) throws RemoteException {

		return a * b;
	}

	@Override
	public int divide(int a, int b) throws RemoteException {

		return a / b;
	}

}

Server.java

package com.dineshkrish.rmi;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {

	public static void main(String[] args) {
		
		final int PORT = 3535;
		
		try {
			
			Arithmatic arithmatic = new ArithmaticImpl();
			
			Registry registry = LocateRegistry.createRegistry(PORT);
			
			registry.bind("arithmatic", arithmatic);
			
			System.out.println("Registry is Created and Running at "+PORT+" port...");
			
		} catch (RemoteException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
		
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		
	}
}

Client.java

package com.dineshkrish.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) {
		
		try {
			
			Arithmatic arithmatic = (Arithmatic)Naming.lookup("rmi://localhost:3535/arithmatic");
			
			Scanner scanner = new Scanner(System.in);
			
			System.out.println("Enter the first number : ");
			int a = scanner.nextInt();
			
			System.out.println("Enter the second number : ");
			int b = scanner.nextInt();
			
			System.out.println("Arithmatic Result of ("+a+" & "+b+")\n----------------------------");
			System.out.println("Addition of "+a+" and "+b+" is : "+arithmatic.add(a, b));
			System.out.println("Subtraction of "+a+" and "+b+" is : "+arithmatic.subtract(a, b));
			System.out.println("Multiplication of "+a+" and "+b+" is : "+arithmatic.multiply(a, b));
			System.out.println("Division of "+a+" and "+b+" is : "+arithmatic.divide(a, b));
			
			scanner.close();
			
		} catch (MalformedURLException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		} catch (RemoteException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		} catch (NotBoundException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
}

Running Server Program

Registry is Created and Running at 3535 port…

Running Client Program

Enter the first number : 30
Enter the second number : 10
Arithmatic Result of (30 & 10)
—————————-
Addition of 30 and 10 is : 40
Subtraction of 30 and 10 is : 20
Multiplication of 30 and 10 is : 300
Division of 30 and 10 is : 3

Download Source Code

Download Here

References

1. Java RMI API JavaDocs