0

我在使用 RMI 和 Tomcat 时遇到了一些困难。这是我正在处理的这个项目的总体情况:RMI 客户端由运行在 Tomcat 中的 Java servlet 调用,servlet 接受用户输入并将参数传递给 RMI 客户端。然后RMI客户端然后调用RMI服务器运行繁重的计算部分。问题是,尽管没有在 Tomcat 服务器中托管 RMI 客户端,一切都可以正常工作,但一旦我将它放入 Tomcat,它就不会运行。以下是一些配置: 我没有在代码中设置安全管理器,我假设 Tomcat 将使用它自己的。

    //if (System.getSecurityManager() == null) {
    //    System.setSecurityManager(new SecurityManager());
    //}

我还将权限设置如下:

$CATALINA_HOME/conf/catalina.policy

grant codeBase "file:${catalina.home}/webapps/MyAPP/WEB-INF/classes/-" { 
    permission java.security.AllPermission "", ""; }; 
grant codeBase "file:${catalina.home}/webapps/MyAPP/WEB-INF/lib/-" { 
    permission java.security.AllPermission "", ""; 
}; 
grant codeBase "file:${catalina.home}/webapps/MyAPP/WEB-INF/lib/some-common-3.0.jar" { 
    permission java.io.FilePermission "*", "read, write"; 
};

除了这两个配置,我没有设置任何 java.rmi.server.codebase 或 java.security.policy。

将代码从 RMI 客户端提交到 RMI 服务器时出现以下错误:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
java.lang.ClassNotFoundException: mypackage.SomeClass
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:334)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at com.sun.proxy.$Proxy19.executeTask(Unknown Source)
at cluster.server.centralservice.CentralManagementServer.submitJob(CentralManagementServer.java:232)
at cluster.server.centralservice.JobSubmitter.runJob(JobSubmitter.java:226)
at cluster.server.centralservice.JobSubmitter.doPost(JobSubmitter.java:144)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
java.lang.ClassNotFoundException: mypackage.SomeClass

我被困在这里几天了。我在网上搜索的大多数示例都已过时,并且没有涵盖设置了 Tomcat 的 RMI。有人可以帮忙吗?非常感谢。

4

1 回答 1

0

正如例外所说

java.lang.ClassNotFoundException: mypackage.SomeClass

发生这种情况是因为 RMI 使用了一个无法看到的单独的类加载器SomeClass

添加<Loader delegate="true"/>到 tomcat/conf 目录中的 context.xml 文件(到 Context 元素)。

此更改解决了我遇到的类似问题(不是特别针对 RMI),但值得一试。

于 2013-06-14T18:22:16.147 回答