示例代码

 
RMIServer.java
import java.rmi.Naming; import java.rmi.registry.LocateRegistry; public class RMIServer { public static void main(String[] args) throws Exception { System.setProperty("java.rmi.server.hostname", "159.138.23.173"); RemoteHello h = new RemoteHello(); LocateRegistry.createRegistry(1099); Naming.bind("rmi://0.0.0.0:1099/hello", h); } }
RemoteHello.java
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class RemoteHello extends UnicastRemoteObject implements IRemoteHello { protected RemoteHello() throws RemoteException{ } public String hello() throws RemoteException { String sysinfo = ""; System.out.println("GET A RMI CALL!"); System.out.println(System.getProperty("user.dir")); sysinfo = System.getProperty("user.dir"); return sysinfo; } }
IRemoteHello.java
import java.rmi.Remote; import java.rmi.RemoteException; public interface IRemoteHello extends Remote { public String hello() throws RemoteException; }
客户端
RMIClient
import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; public class RMIClient { public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException { // IRemoteHello hello = (IRemoteHello) Naming.lookup("rmi://127.0.0.1:1099/hello"); IRemoteHello hello = (IRemoteHello) Naming.lookup("rmi://159.138.23.173:1099/hello"); String ret = hello.hello(); System.out.println(ret); } }