我的代码是指servlet自己的目录中的接口和存根(在顶部,在'WEB-INF / classes'中。存根是在RMI编译期间创建的,我可以查看它们存在于同一文件夹中servlet驻留。但是,在运行servlet时,无法引用存根。我从servlet本身内部启动注册表。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("test3");
try {
try {
LocateRegistry.createRegistry(5002);
TestController obj = new TestController();
Naming.rebind("CerberusServer", obj);
} catch(Exception e) {
System.out.println("EXCEPTION::::: New Servlet2:: 2:: " + e);
System.out.println(e.getMessage());
}
} catch(Exception e) {
System.out.println("unable to run RMI: " + e);
}
}
测试控制器:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class TestController extends UnicastRemoteObject implements ITestController {
protected TestController() throws RemoteException {
}
@Override
public String test(String text) throws RemoteException, Exception {
return text;
}
}
ITestController:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ITestController extends Remote {
String test(String text) throws RemoteException, Exception;
}
尝试绑定时得到的确切异常是
java.rmi.ServerException:服务器线程中发生了RemoteException;嵌套异常是: java.rmi.UnmarshalException:错误解组参数;嵌套异常是: java.lang.ClassNotFoundException:TestController_Stub(没有安全管理器:禁用RMI类加载器)
编辑:它与running rmi server, classnotfound不同,因为此错误发生在Tomcat servlet应用程序中。如果没有servlet部分,则不会出现此错误。