示例代码

包结构
└── com ├── Client │   └── RMIClient.java └── Server ├── IRemoteHello.java ├── RMIServer.java └── RemoteHello.java
RMIClient.java
package com.Client; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import com.Server.IRemoteHello; public class RMIClient { public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException { IRemoteHello hello = (IRemoteHello) Naming.lookup("rmi://127.0.0.1:1099/hei"); // IRemoteHello hello = (IRemoteHello) Naming.lookup("rmi://159.138.23.173:1099/hello"); System.out.println(hello.getClass()); System.out.println("test".getClass()); String ret = hello.hello(); System.out.println(ret); } }
IRemoteHello.java
package com.Server; import java.rmi.Remote; import java.rmi.RemoteException; public interface IRemoteHello extends Remote { public String hello() throws RemoteException; }
RemoteHello.java
package com.Server; 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; } }
RMIServer.java
package com.Server; 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/hei", h); } }