Using Intelij RMI & Netbean:
Interface: RmiServerIntf.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RmiServerIntf extends Remote{
public String getMessage() throws RemoteException;
}
//Implementation: RmiImp.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class RmiImp extends UnicastRemoteObject implements RmiServerIntf {
public static final String MESSAGE = "Hello World";
public RmiImp() throws RemoteException {
super(0);
}
public String getMessage() {
return MESSAGE;
}
}
//Server:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RmiServer {
public static void main(String[] args) throws RemoteException, MalformedURLException {
System.out.println("RmiServer Strarted");
try{
LocateRegistry.createRegistry(1099);
System.out.println("java RMI registry created");
RmiImp obj = new RmiImp();
Naming.rebind("rmi://localhost/RmiServer",obj);
System.out.println("PeerServer bound in registy");
}
catch(RemoteException re){
System.out.println("java RMI registry already exist");
}
}
}
//Client:
public class RmiClient {
public static void main(String[] args) throws Exception {
RmiServerIntf obj = (RmiServerIntf) Naming.lookup("rmi://localhost/RmiServer");
System.out.println(obj.getMessage());
}
}