调用ejb出现java.lang.ClassCastException异常

时间:2023-01-25 20:34:42

我用eclipse4.3运行ejb一个小实例,出现了这个异常,搞了很久

我在web应用导入ejb的jar的文件,然后调用ejb。就出现了这个异常。

我的jsp文件如下:

<%
String str = request.getParameter("name");

if(str==null)str = "第一个ejb-local";

System.out.println("+++++++=====");


try {
Context context = new InitialContext();
final String appName = "";
//部署的jar文件的名字
final String moduleName = "HelloWordLocalBean";
final String distinctName = "";

//实现类的类名
final String beanName = HelloWorldBean.class.getSimpleName();
//接口类的全名
final String viewClassName = HelloWorld.class.getName();

System.out.println(viewClassName);

String lookupStr = "ejb:" + appName + "/"
+ moduleName + "/" + distinctName + "/" + beanName + "!"
+ viewClassName;
HelloWorld he = (HelloWorldBean)context.lookup(lookupStr);
out.println(he.sayHello(str));
} catch (NamingException e) {
e.printStackTrace();
return;
}
%>


调用ejb文件如下

接口

package com.ibytecode;
import javax.ejb.Local;
import javax.ejb.Remote;
@Local
public interface HelloWorld {
public String sayHello(String name);
}



实现类

package com.ibytecode.businesslogic;
import javax.ejb.Local;
import javax.ejb.Stateless;
import com.ibytecode.HelloWorld;
@Stateless
@Local(HelloWorld.class)
public class HelloWorldBean implements HelloWorld {
    public HelloWorldBean() {System.out.println("[*]");}
public String sayHello(String name) {
// TODO Auto-generated method stub
return name+"Hello World!!!---local11";
}
}

在网上试了很多方法都不可以。

java.lang.ClassCastException: com.sun.proxy.$Proxy16 cannot be cast to com.ibytecode.businesslogic.HelloWorldBean


20:08:09,330 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/TestServer].[jsp]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: java.lang.ClassCastException: com.sun.proxy.$Proxy16 cannot be cast to com.ibytecode.businesslogic.HelloWorldBean

at org.apache.jsp.Test_jsp._jspService(Test_jsp.java:96)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]

at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_15]



最后,我在导出jar文件时候,我选择了普通的jar文件导入,并且只导出java文件和classpath配置和project配置,不导出xml文件就可以了


调用ejb出现java.lang.ClassCastException异常调用ejb出现java.lang.ClassCastException异常

最后可以重新运行了