import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class DisplayPerfectTime {
public static void main(String[] args) throws Exception {
System.setSecurityManager(new RMISecurityManager());
PerfectTimeI t = (PerfectTimeI) Naming.lookup("/PerfectTime");
for (int i = 0; i < 10; i++)
System.out.println("Perfect time = " + t.getPerfectTime());
}
}
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI {
public PerfectTime() throws RemoteException {
super(); // Called automatically
}
public long getPerfectTime() throws RemoteException {
return System.currentTimeMillis();
}
public static void main(String[] args) throws Exception {
PerfectTime pt = new PerfectTime();
Naming.rebind("/PerfectTime", pt);
System.out.println("Ready to do time");
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface PerfectTimeI extends Remote{
long getPerfectTime() throws RemoteException;
}
register.policy
grant{permission java.security.AllPermission;};
运行步骤
F:\rmi>javac *.java
F:\rmi>rmic PerfectTime
F:\rmi>start rmiregistry
F:\rmi>java PerfectTime
Ready to do time
另开一DOS窗口
F:\rmi>java -Djava.security.policy=registerit.policy DisplayPerfectTime
Perfect time = 1011585443953
Perfect time = 1011585443968
Perfect time = 1011585444000
Perfect time = 1011585444000
Perfect time = 1011585444015
Perfect time = 1011585444031
Perfect time = 1011585444031
Perfect time = 1011585444046
Perfect time = 1011585444062
Perfect time = 1011585444062
注意:如果在rmic PerfectTime时报找不到PerfectTime类,请在环境变量中的ClassPath中加入:F:\rmi
import java.rmi.RMISecurityManager;
public class DisplayPerfectTime {
public static void main(String[] args) throws Exception {
System.setSecurityManager(new RMISecurityManager());
PerfectTimeI t = (PerfectTimeI) Naming.lookup("/PerfectTime");
for (int i = 0; i < 10; i++)
System.out.println("Perfect time = " + t.getPerfectTime());
}
}
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI {
public PerfectTime() throws RemoteException {
super(); // Called automatically
}
public long getPerfectTime() throws RemoteException {
return System.currentTimeMillis();
}
public static void main(String[] args) throws Exception {
PerfectTime pt = new PerfectTime();
Naming.rebind("/PerfectTime", pt);
System.out.println("Ready to do time");
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface PerfectTimeI extends Remote{
long getPerfectTime() throws RemoteException;
}
register.policy
grant{permission java.security.AllPermission;};
运行步骤
F:\rmi>javac *.java
F:\rmi>rmic PerfectTime
F:\rmi>start rmiregistry
F:\rmi>java PerfectTime
Ready to do time
另开一DOS窗口
F:\rmi>java -Djava.security.policy=registerit.policy DisplayPerfectTime
Perfect time = 1011585443953
Perfect time = 1011585443968
Perfect time = 1011585444000
Perfect time = 1011585444000
Perfect time = 1011585444015
Perfect time = 1011585444031
Perfect time = 1011585444031
Perfect time = 1011585444046
Perfect time = 1011585444062
Perfect time = 1011585444062
注意:如果在rmic PerfectTime时报找不到PerfectTime类,请在环境变量中的ClassPath中加入:F:\rmi