import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteInterface extends Remote {
String sayHello() throws RemoteException;
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface {
protected RemoteObject() throws RemoteException {
super();
}
@Override
public String sayHello() throws RemoteException {
return "Hello from remote object!";
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server {
public static void main(String[] args) {
try {
RemoteInterface remoteObject = new RemoteObject();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("RemoteObject", remoteObject);
System.out.println("Server started.");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost");
RemoteInterface remoteObject = (RemoteInterface) registry.lookup("RemoteObject");
String result = remoteObject.sayHello();
System.out.println("Response from server: " + result);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
}
}
}