我在Eclipse开发模式下运行Google AppEngine(1.9.8)项目时遇到ClassCastException,否则它可以作为一个独立的java应用程序运行。
当尝试使用Class对象创建新实例时,下面的方法会在最后一行抛出异常。
public FixGroupMgr getGroupMgr(String fixVersion)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
String versionIdent = fixVersion.replaceAll("[\",.]", "") + "Mgr";
FixGroupMgr.cleanStaticData();
Class<?> c =
ClassLoader.getSystemClassLoader().loadClass(
"com.globalforge.infix." + versionIdent);
return (FixGroupMgr) c.newInstance();
}
versionIdent =&#34; FIX44Mgr&#34;在调用replaceAll()之后。
com.globalforge.infix.FIX44Mgr
extends
com.globalforge.infix.FixGroupMgr
我已经确认两个类文件都存在于我放在WEB-INF / lib中的jar文件中。
[1378880913@qtp-935467774-0] ERROR com.globalforge.infix.FixRulesTransformVisitor - Bad message or unsupported fix version....parser HALT.
java.lang.ClassCastException: com.globalforge.infix.FIX44Mgr cannot be cast to com.globalforge.infix.FixGroupMgr
at com.globalforge.infix.FixContextMgr.getGroupMgr(FixContextMgr.java:43)
at com.globalforge.infix.FixMessageMgr.init(FixMessageMgr.java:157)
at com.globalforge.infix.FixMessageMgr.parseField(FixMessageMgr.java:138)
at com.globalforge.infix.FixMessageMgr.parseMessage(FixMessageMgr.java:112)
at com.globalforge.infix.FixMessageMgr.<init>(FixMessageMgr.java:50)
at com.globalforge.infix.FixRulesTransformVisitor.visitParseRules(FixRulesTransformVisitor.java:68)
at com.globalforge.infix.FixRulesTransformVisitor.visitParseRules(FixRulesTransformVisitor.java:31)
at com.globalforge.infix.antlr.FixRulesParser$ParseRulesContext.accept(FixRulesParser.java:85)
at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:44)
at com.globalforge.infix.FixRulesEngine.transformMsg(FixRulesEngine.java:99)
at com.globalforge.infix.server.ExampleAppServiceImpl.transformMsg(ExampleAppServiceImpl.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:265)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:305)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.googlecode.objectify.cache.AsyncCacheFilter.doFilter(AsyncCacheFilter.java:58)
at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:127)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:491)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
答案 0 :(得分:0)
确保此类不在两个不同的罐子中。例如,如果仍然存在较旧版本的jar,则可能会出现此错误。
答案 1 :(得分:0)
我能够通过避免系统类加载器来解决这个问题。
作为一个健全性测试,我在Tomcat 7 Web容器中部署了我的应用程序,并且惊讶地发现在同一个方法中抛出了类似的异常(ClassNotFoundException)。
作为一种解决方法,我改为使用加载类的类加载器进行调用并且它起作用。然后我将修复程序部署到Google AppEngine,它也有效。
稍微探讨之后,我发现许多Web容器将系统类加载器与可能导致这些类型行为的应用程序隔离开来。
以下是修复:
public FixGroupMgr getGroupMgr(String fixVersion)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
String versionIdent = fixVersion.replaceAll("[\",.]", "") + "Mgr";
FixGroupMgr.cleanStaticData();
/*
* Class<?> c = ClassLoader.getSystemClassLoader().loadClass(
* "com.globalforge.infix." + versionIdent);
*/
Class<?> c =
FixContextMgr.class.getClassLoader().loadClass(
"com.globalforge.infix." + versionIdent);
return (FixGroupMgr) c.newInstance();
}