2011-03-10 100 views
1

我创建了一个bean对象,并在运行时遇到这个错误的客户端,EJB异常,同时尝试运行

java.lang.ClassCastException at 
com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(Unknown Source) at 
javax.rmi.PortableRemoteObject.narrow(Unknown Source) at Client_TestPortal.main Client_TestPortal.java:71) Caused by: 
java.lang.ClassCastException: javax.naming.Reference ... 3 more 

如何在我的客户端创建bean对象?我有一个豆界面,TestPortal,和一个豆类,TestPortalBean,在耳朵,PortalEJB

下面是代码中,我使用来创建在客户端EJB实例,

String sEjbRemote = "PortalEJB/TestPortalBean/remote"; 

Properties pProp = new Properties(); 
pProp.put("java.naming.factory.initial",sInitCtxtCls); 
pProp.put("java.naming.provider.url", sUrl); 

javax.naming.InitialContext initialContext = new InitialContext(pProp); 
Object ref = initialContext.lookup(sEjbRemote); 
System.out.println("\n\n \t Source :::"+ref.toString()); 

test.ejb.TestPortal testportal = (test.ejb.TestPortal)PortableRemoteObject.narrow(ref,test.ejb.TestPortal.class); 

体REF = initialContext.lookup(sEjbRemote);

当我打印对象在SOP ref.toString();

我得到了以下信息,但我不能够创建TestPortal对象,它是在被部署在JBOSS- AS版本PoratlEJB.ear:Jboss-5.0.1.GA

 Source :::Reference Class Name: Proxy for: test.ejb.TestPortal 

类型:ProxyFactoryKey 含量:ProxyFactory里/ TestPortalBean/PortalEJB/TestPortalBean /远程 类型:EJB容器名称 含量:jboss.j2ee:耳= PortalEJB.ear,罐子= PortalEJB.jar,名字= TestPortalBean,SERV 冰= EJB3 类型:代理工厂是本地 内容:假 类型:远程商业I覆盖整个院落 内容:test.ejb.TestPortal 类型:远程托管URL 内容:插座:// s9458:3973/

+0

你是如何创造客户对象?在这里显示一些代码。 – 2011-03-10 09:47:24

+0

你在用什么? J2ee 1.4? Java EE 5/6? Web界面?远程富客户端? – Puce 2011-03-10 09:48:59

回答

1

通常情况下,我们做这样的事情,

Properties props = getConfigurationProps(); 
InitialContext ctx = new InitialContext(props); 
tp = (TestPortal) ctx.lookup(TEST_PORTAL_JNDI_NAME); 

[编辑]

从你的代码,我可以看到你正在试图narrow()的对象。让我们来看看什么文档说对此,它说,


Checks to ensure that an object of a remote or abstract interface type can be cast to a desired type. 

Parameters: 
    narrowFrom - the object to check. 
    narrowTo - the desired type. 
Returns: 
    an object which can be cast to the desired type. 
Throws: 
    ClassCastException - if narrowFrom cannot be cast to narrowTo. 

我不知道你什么也最多。但首先,您可以始终这样做,正如我在第一次尝试中已经表明的那样。

TestPortal ref = (TestPortal) initialContext.lookup(sEjbRemote); 

代替,

Object ref = initialContext.lookup(sEjbRemote); 
+0

@saran:将您的代码添加到您的原始问题中。您可以通过点击问题左下角的edite链接来做同样的事情。 – 2011-03-10 10:18:13

+0

@Saran:现在你可以从这里删除你的评论,因为他们没有更多的相关性。我也会这样做,包括这个。 – 2011-03-10 10:27:21

+0

@Saran:我已经更新了我的答案,因此,看完你的代码后。 – 2011-03-10 10:27:50