Cross-Platform, Java

A Simple Java RMI example

Using Java RMI, the methods of remote objects can be invoked from other jvms, possibly in different hosts!
Let’s see a simple but complete example.

Writing the interface
This is the contract between server and clients!

1
2
3
4
5
6
7
8
9
10
11
package my.rmi.example;
 
import java.rmi.Remote;
import java.rmi.RemoteException;
 
public interface MathService 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;
}

Writing the server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package my.rmi.example;
 
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
 
 
public class MathServer extends UnicastRemoteObject implements MathService{
 
    public MathServer() throws RemoteException{
        super();
    }
 
    @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; //check for division with zero here!
    }
 
    public static void main(String args[]){
        if (System.getSecurityManager() == null)
            System.setSecurityManager ( new RMISecurityManager() );
        try{
            LocateRegistry.createRegistry(1099); 
            MathServer svr = new MathServer();
            Naming.bind ("MathService", svr);
            System.out.println ("Service started....");
        }
        catch(RemoteException re){
            System.err.println(re.getMessage());
        }
        catch(AlreadyBoundException abe){
            System.err.println(abe.getMessage());
        }
        catch(MalformedURLException mue){
            System.err.println(mue.getMessage());
        }
    }
}

Writing the client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package my.rmi.example;
 
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
 
public class MathClient {
    public static void main(String args[]){
        if (System.getSecurityManager() == null)
        {
            System.setSecurityManager (new RMISecurityManager());
        }
 
        MathService service = null;
        try {
            service = (MathService) Naming.lookup("//localhost/MathService");
            System.out.println  ("Add : " + service.add(2,2));
            System.out.println  ("Subtract : " + service.subtract(5,2));
            System.out.println  ("Multiply : " + service.multiply(2,6));
            System.out.println  ("Divide : " + service.divide(4,2));
        } catch (NotBoundException ex) {
            System.err.println(ex.getMessage());
        } catch (MalformedURLException ex) {
            System.err.println(ex.getMessage());
        } catch (RemoteException ex) {
            System.err.println(ex.getMessage());
        }
    }
}

Writing the server policy file

grant {
    permission java.net.SocketPermission "localhost:*", "connect,resolve";
    permission java.net.SocketPermission "localhost:*", "accept";
};

Writing the client policy file

grant {
    permission java.net.SocketPermission "localhost:*", "connect,resolve";
};

Running the server

java -Djava.security.policy=server_policy_file my.rmi.example.MathServer

Running the client

java -Djava.security.policy=client_policy_file my.rmi.example.MathClient