站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题按照书上作第一个EJB例子,运行报ClassCaseException

按照书上作第一个EJB例子,运行报ClassCaseException

摘要: 按照书上作第一个EJB例子,运行报ClassCaseException


刚开始学习EJB,按照书上作了一个EJB的例子,但是运行时总是报ClassCaseException,查了很多资料,也在网上查了一直没有找到原因,请各位高手指教!
部署工具:deploytool 服务器j2EE1.4
运行客户端报错信息:
java.lang.ClassCastException
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableR
emoteObject.java:294)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
at ConverterClient.main(ConverterClient.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:28
5)
at com.sun.enterprise.appclient.Main.(Main.java:420)
at com.sun.enterprise.appclient.Main.main(Main.java:92)
在将类转换成主接口出现ClassCaseException,但不知道是什么原因。
import converter.Converter;
import converter.ConverterHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.math.BigDecimal;
public class ConverterClient {
public static void main(String[] args) {
try {
Context initial = new InitialContext();
Context myEnv = (Context) initial.lookup("java:comp/env");
Object objref = myEnv.lookup("SimpleConverter");

ConverterHome home =
(ConverterHome) PortableRemoteObject.narrow(objref,
ConverterHome.class);
Converter currencyConverter = home.create();
BigDecimal param = new BigDecimal("100.00");
BigDecimal amount = currencyConverter.dollarToYen(param);

System.out.println(amount);
amount = currencyConverter.yenToEuro(param);
System.out.println(amount);
System.exit(0);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
我设置时定义的引用名称为SimpleConverter,JNDI名称设置为MyConverter,不能运行,后修改为SimpleConverter还是不能运行!
远程接口:package converter;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.*;
public interface Converter extends EJBObject {
public BigDecimal dollarToYen(BigDecimal dollars) throws RemoteException;

public BigDecimal yenToEuro(BigDecimal yen) throws RemoteException;
}
主接口:
package converter;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface ConverterHome extends EJBHome {
Converter create() throws RemoteException, CreateException;
}
EJB
package converter;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.math.*;
public class ConverterBean implements SessionBean {
BigDecimal yenRate = new BigDecimal("121.6000");
BigDecimal euroRate = new BigDecimal("0.0077");

public ConverterBean() {
}
public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);

return result.setScale(2, BigDecimal.ROUND_UP);
}
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);

return result.setScale(2, BigDecimal.ROUND_UP);
}
public void ejbCreate() {
}

public void ejbRemove() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}

public void setSessionContext(SessionContext sc) {
}
}



Context myEnv = (Context) initial.lookup("java:comp/env");
Object objref = myEnv.lookup("SimpleConverter");
改为:
Object objref = initial.lookup("SimpleConverter");
就可以了




按照你说的改过了,但是还是报同样的异常。我按照下面的改,问题依旧。
InitialContext ctx=new InitialContext();
Object objref=ctx.lookup("java:comp/env/SimpleConverter");
非常感谢你提供的意见!


web.xml有没有配置正确ejb-ref?


我是通过Deploytool进行的打包部署,还需要再手动修改吗?包里没有web.xml,是客户端程序,并没有Web应用,所以可能没有web.xml吧。
运行时显示objref.getClass()为 com.sun.corba.ee.impl.corba.CORBAObjectImpl,这个类正确吗?还是应该返回ConverterBean的对象阿?


论坛里这么多高手,怎么也没人帮帮小弟阿!我搞了好几天了,还没搞明白。使我的设置那里有问题吗?


调用的地方需要有一个申明该调用的ejb-ref,在web里是写在web.xml中,其他的就不知道写在哪里了。


呵呵,谢谢各位了,我的问题已经解决了!不知道是不是因为我用的是J2EE1.4,而书上用的是1.3的原因。书上设的引用名是SimpleConverter,而JNDI的名字是MyConverter。按照书上的设置会报NameNotFoundException。
我的解决办法是:
将JNDI的名字设置为SimpleConverter。并且设置一个环境变量
set APPCPATH=D:/java/ConverterAppClient.jar
我曾经将这个客户端的JAR放到CLASSPATH中,但是没有起作用。
然后再运行程序,成功!
谢谢各位的帮助!



↑返回目录
前一篇: 提问:SQLServer数据库转ORACLE,WebLogic7.01后台报错,到底是什么原因呢?
后一篇: 为什么我在jbuilder2006中编译idl文件报错,无法编译---Invocation Error. Check your CORBA configuration....