Airthematic Operation in Java RMI | Add Two Number Java RMI Program

To write a program for addition of two numbers using RMI (Remote Method Invocation).
Step by step procedure Algorithm For Java RMI Structure
• The client program does not have local access to the class from which a local or remote object was instantiated RMI services can download the class file.
Steps:
• Define the remote interface
• Implement the server
• Implement the client
• Compile the source files
• Start the Java RMI registry, server, and client
The files needed for this program are:
• addintf.java - a remote interface
• addServer.java - a remote object implementation that implements the remote interface
• addClient.java - a simple client that invokes a method of the remote interface
• addImpl.java-a simple implementation file.
Implement the server
The implementation class add Server implements the remote interface addIntf, providing an implementation for the remote method. The method does not need to declare that it throws any exception because the method implementation itself does not throw Remote Exception nor does it throw any other checked exceptions.
Compile the source files
The source files for this example can be compiled as follows:
javac addIntf2.java
javac addImpl2.java
javac addServer2.java
javac addClient2.java
Generate Stubs and Skeletons
To create stub and skeleton files, run the rmic compiler on the names of compiled class files that contain remote object implementations. rmic takes one or more class names as input and produces as output class files of the form Server _Skel.class and Server _Stub.class.
rmic addImpl
the preceding command creates the following stub and skeleton files
• Server _Stub.class
• Server _Skel.class
Start the Java RMI registry, server, and client
To run this example, you will need to do the following:
• Start the Java RMI registry
• Start the server
• Run the client
Start the Java RMI registry
Before start the structure of RMI Registry we should be set path of the registry, type the following command
Set path=c:\jdk1.5\bin
To start the registry, run the rmiregistry command on the server's host. This command produces no output (when successful) and is typically run in the background.
For example, on Windows platforms:
start rmiregistry
1. Finally the structure of  RMI program is executed as shown in the output window.
Source Code Programming
a) Server Program
import java.rmi.*;
import java.net.*;
public class AddServer2
{
public static void main(String arg[])throws RemoteException
{
try
{
AddImpl2 obj=new AddImpl2();
Naming.rebind("AddServer2",obj);
}
catch(Exception e)
{}
}
}
b) Client Program
import java.rmi.*;
import java.net.*;
public class AddClient2
{
public static void main(String arg[])throws Exception
{
try
{
String addServerURL="rmi://"+arg[0]+"/AddServer2";
AddIntf2 oo=(AddIntf2)Naming.lookup(addServerURL);
double d1=Double.valueOf(arg[1]).doubleValue();
double d2=Double.valueOf(arg[2]).doubleValue();
System.out.print("Output ");
System.out.println(oo.add(d1,d2));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
c) Interface Program
import java.rmi.*;
public interface AddIntf2 extends Remote
{
double add(double d1,double d2) throws RemoteException;
}
d) Implementation Program
import java.rmi.*;
import java.rmi.server.*;

public class AddImpl2 extends UnicastRemoteObject implements AddIntf2
{
public AddImpl2()throws RemoteException
{}
public double add(double d1,double d2) throws RemoteException
{
return (d1+d2);
}
}
EJB And Java RMI program

Related post