这是界面
package Server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RmiSomme extends Remote{
public abstract void getInformation(String nom, String prenom, int age) throws RemoteException;
}
这里是实现
package Server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class RmiSommeImpl extends UnicastRemoteObject implements RmiSomme{
private static final long serialVersionUID = 1L;
protected RmiSommeImpl() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
@Override
public void getInformation(String nom, String prenom, int age) throws RemoteException {
// TODO Auto-generated method stub
System.out.println(nom + " " + prenom);
}
}
这是服务器类
package Server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class Server {
public static void main(String[] args) throws RemoteException, MalformedURLException {
try {
RmiSommeImpl rmi = new RmiSommeImpl();
LocateRegistry.createRegistry(11);
Naming.rebind("rmi://localhost:11/RMI", rmi);
System.out.println(rmi.toString());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
当我启动服务器类时,此错误不断出现:
无法使公共抽象 void Server.RmiSomme.getInformation(java.lang.String,java.lang.String,int) 抛出 java.rmi.RemoteException 可访问:模块 RMIServer 不会“导出服务器”到模块 java.rmi
您必须通过将以下行添加到 module-info.java 文件中来导出 Server 包:
exports Server to java.rmi;
您需要通过将以下行添加到 module-info.java 文件中来导出 package_name :
将 package_name 导出到 java.rmi;