class AdderClient
void adder(int a, int b)
try
AdderInterface adder =
(AdderInterface) Naming.lookup("Adder")
System.out.println(
a + "+" + b + "=" + adder.add(a, b))
catch Exception e
System.err.println(
"Exception" + e.getMessage())
e.printStackTrace()
import java.rmi.Remote import java.rmi.RemoteException public interface AdderInterface extends Remote int add(int a, int b) throws RemoteException
Remote.
RemoteException.
import java.rmi.RemoteException
import java.rmi.server.UnicastRemoteObject
public class AdderImplementation
extends UnicastRemoteObject
implements AdderInterface
AdderImplementation() throws RemoteException
super()
int add(int a, int b) throws RemoteException
return a + b
rmic creates remote-object proxies.
* ls *.class AdderImplementation.class * rmic -v1.2 AdderImplementation * ls *.class AdderImplementation.class AdderImplementation_Stub.class *
rmiresistry is a simple registration-lookup service.
rmiresistry maps a string to a remote object and its proxy.
import java.rmi.Naming
class AdderRegister
public static void
main(String[] args)
try
Naming.rebind(
"Adder", new AdderImplementation())
catch (Exception e)
System.err.println(
"rebind() error: " + e.getMessage())
e.printStackTrace()
rmiresistry remote objects via their proxies to
return the proxy (if any) bound to a string.
rmiresistry service names look like URLs.
import java.rmi.Naming
class Adder
final AdderInterface adder
Adder()
try
adder = (AdderInterface) Naming.lookup(
"rmi://clayton.cs.monmouth.edu:5370/Adder")
catch Exception e
System.err.println(e.getMessage())
e.printStackTrace()
int adder(int a, int b)
int sum = 0
try
sum = adder.add(a, b)
catch Exception e
System.err.println(e.getMessage())
e.printStackTrace()
return sum
SecurityManager.
grant {
permission
java.net.SocketPermission "*:1024-65535",
"connect,accept,resolve";
};
public static void
main(String args[])
if System.getSecurityManager() == null
System.setSecurityManager(
new RMISecurityManager())
final AdderClient ac = new AdderClient()
System.out.println("2+3 =", ac.add(2, 3))
Serializable interface allows I-O on class
instances.
This page last modified on 16 April 2004.