小弟刚开始学J2EE,安装完j2eesdk-1_4_03-windows.exe和Eclipse,从网上找了一个小例子“HelloWorld”作实践,使用deploytool部署完毕后,启动Client调用时出了个问题:RemoteHome home=(RemoteHome)javax.rmi.PortableRemoteObject.narrow(obj,RemoteHome.class);//程序在此处时,发现home=null。还请各位大虾帮帮看看:
Client.java
public class Client
{
public static void main(String[] args)
{
try
{
Properties prop = new Properties();
prop.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
prop.setProperty("java.naming.provider.url","iiop://localhost:3700");
System.out.println("set prop success");
javax.naming.Context initContext=new javax.naming.InitialContext(prop);
System.out.println("get initContext success");
Object obj=initContext.lookup("ejb/hello");
if(obj!=null){
System.out.println("lookup jndi success");
}
RemoteHome home=(RemoteHome)javax.rmi.PortableRemoteObject.narrow(obj,RemoteHome.class);//程序在此处时,发现home=null。
if(home!=null){
System.out.println("ini remotehome success");
RemoteInterface remote=home.create();
String receiveFromRemote=remote.message("Hello,Remote Object!");
System.out.println("Client Received From Remote Object: \""+receiveFromRemote+"\"");
}else{
System.out.println("ini remotehome error");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
附:
RemoteHome.java
public interface RemoteHome extends javax.ejb.EJBHome
{
RemoteInterface create()throws java.rmi.RemoteException,
javax.ejb.CreateException;
}
RemoteInterface .java
public interface RemoteInterface extends javax.ejb.EJBObject
{
public String message(String str)throws java.rmi.RemoteException;
}
RemoteObject.java
public class RemoteObject implements javax.ejb.SessionBean
{
public String message(String str)throws java.rmi.RemoteException
{
System.out.println("Remote Object Received From Client:\""+str+"\""); //打印(从客户端)接收到的字符串。
return "Hello,I'm Remote Object,I received your message:\'"+str+"\'"; //返回一应答字符串。
}
public RemoteObject() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(javax.ejb.SessionContext sc) {}
}