这个项目问题快让我白了头发

06-05-20 食铁兽

各位同志,我在做一个j2ee的小项目,什么都做好了,就连部署都做好了,可是到后来运行的时候却有异常。
我的JDK是1.4.2,J2EE是1.3.1。
问题描述如下:
我这个项目由四个文件构成:RemoteInterface.java-远程接口,LocalInterface.java-远程主接口,RemoteEJB-EJB类,LocalClient.java-客户端文件。
问题出在客户端文件运行时。
我把j2ee服务器打开,而且部署也成功了,部署的时候生成的客户端jar文件就和客户端文件放在同一目录下,
但是在运行客户端文件的时候,它老是抛出ClassCastException,我查过了,这个异常是由于这句语句
LocalInterface objLocalInterface=(LocalInterface)PortableRemoteObject.narrow(objObject,LocalInterface.class)
错误引起的,是由于该语句无法在对于EJB的引用中转化得到远程主接口,转化失败就抛出ClassCastException异常。


我知道问题大致就是出在这个地方,可就是不知道该怎样解决,网上也找不到答案。
我现在每根头发都疼。
在此拜求各位帮忙解决,不胜感谢。


这是远程接口代码
/*
* RemoteInterface.java
*
* Created on 2006年4月29日, 下午7:17
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

//package search;

/**
*
* @author ShenChen
*/

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;

public interface RemoteInterface extends EJBObject
{
public boolean verifyClient(String name,String password) throws RemoteException,SQLException;
public void searchProfiles(String name) throws RemoteException,SQLException;
public int getVectorlength() throws RemoteException;
public Vector getNamevector() throws RemoteException;
public Vector getSexvector() throws RemoteException;
public Vector getBloodtypevector() throws RemoteException;
public Vector getHeightvector() throws RemoteException;
public Vector getWeightvector() throws RemoteException;
public Vector getKindvector() throws RemoteException;
public Vector getFingerprintvector() throws RemoteException;
public Vector getDnavector() throws RemoteException;
}


这是远程主接口
/*
* LocalInterface.java
*
* Created on 2006年4月29日, 下午7:20
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

//package search;

/**
*
* @author ShenChen
*/


import javax.ejb.EJBHome;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;

public interface LocalInterface extends EJBHome
{
RemoteInterface create()throws RemoteException,CreateException;
}

接下来的远程EJB代码实现了远程接口中的所有方法,应该不会有什么问题,就算有也不至于抛出那个异常。我就不把代码贴出来了,有点长。

还有就是出问题的客户端代码。比较长,我只贴关键那部分
Context objContext=new InitialContext();
Object objObject=objContext.lookup("RemoteEJBJNDI");
//System.out.println("good");
LocalInterface objLocalInterface=(LocalInterface)PortableRemoteObject.narrow(objObject,LocalInterface.class);
//上面这句就是抛出异常的地方
RemoteInterface objRemoteInterface=objLocalInterface.create();
boolean objBoolean= objRemoteInterface.verifyClient(txtClientName.getText(),txtClientPassword.getText());


下面是另一个网友给出的答案,但是我觉得问题不是他说的那样,

当前者的域小于后者的时候出现
譬如说:前者A是子类的对象,而后者B是父类的对象
coding时,若使用A = B;就会抛出java.lang.ClassCastException
如果你确定是
LocalInterface objLocalInterface=(LocalInterface)PortableRemoteObject.narrow(objObject,LocalInterface.class)
出的问题,那你好好检查一下看我说的是不是对的吧~

拜请banq和各位高手帮我解决这个问题,这问题再拖下去我要气绝身亡了。^_^



banq
2006-05-21 09:20

不要和技术斗气,这些都不值得一气。

EJB不但是编码,关键还在于配置,你要确定ejb-jar.xml中该LocalInterface.class配置是否正确;确认是否是remote接口,而不是local接口。

第二:确认名为RemoteEJBJNDI的JNDI名称是否对应于LocalInterface,应该在jboss.xml或weblogic.xml等配置中有JNDI名称配置,再对照ejb-jar.xml中,是否一一对应。

第三:使用一个没有任何业务编码的简单LocalInterface.class测试一下。

第四:确保客户端可以连接到JNDI服务器,jndi.properties等必须在客户端,或者,在客户端代码中写入JNDI服务器的IP地址等。

主要问题是:远程调用出错,有可能为空,有可能是其他对象,所以导致ClassCastException