H
h_o_u_l_i
Unregistered / Unconfirmed
GUEST, unregistred user!
我照例子做了一个简单的helloWorld的ejb,例子:
/**
* 第一步:
* 定义一个新的接口继承javax.ejb.EJBObject。新定义的接口中的每一个方法都必须抛出
* java.rmi.RemoteException异常。
*/
public interface RemoteInterface extends javax.ejb.EJBObject
{
public String message(String str)throws java.rmi.RemoteException;
}
RemoteObject.java
/**
* 第二步:
* 定义一个类来实现javax.ejb.SessionBean接口。并在该类中实现在第一步中编写的接口中所定义的方法。
*/
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) {}
}
RemoteHome.java
/**
* 第三步:
* 定义一个类继承javax.ejb.EJBHome 。
*/
public interface RemoteHome extends javax.ejb.EJBHome
{
RemoteInterface create()throws java.rmi.RemoteException,javax.ejb.CreateException;
}
Client.java
/**
* 第四步:
* 定义客户端类。
*/
public class Client
{
public static void main(String[] args)
{
try
{
javax.naming.Context initContext=new javax.naming.InitialContext();
Object obj=initContext.lookup("HelloWorld");
//远程查找,由名字得到对应的对象。
RemoteHome home=(RemoteHome)javax.rmi.PortableRemoteObject.narrow(obj,RemoteHome.class);
RemoteInterface remote=home.create();
String receiveFromRemote=remote.message("Hello,Remote Object!");
//远程方法调用
System.out.println("Client Received From Remote Object: /""+receiveFromRemote+"/"");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
用j2ee sdk 1.4 RI部署完成后,用C:/ HelloWorld>java -classpath %J2EE_HOME%/lib/j2ee.jar;.;HelloWorldClient.jar;
Client命令运行,出现下面的错误(捕获后用e.printStackTrace打印):
javax.naming.NoInitialContextException:Need to specify class name in environment or system property,or as an applet parameter, or in an application resource file:java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(initialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at Client.main(Client.java:10)
好象不是EJB的错误,是javax.naming的事但我该怎么改! 折腾半天还不行!!!急
/**
* 第一步:
* 定义一个新的接口继承javax.ejb.EJBObject。新定义的接口中的每一个方法都必须抛出
* java.rmi.RemoteException异常。
*/
public interface RemoteInterface extends javax.ejb.EJBObject
{
public String message(String str)throws java.rmi.RemoteException;
}
RemoteObject.java
/**
* 第二步:
* 定义一个类来实现javax.ejb.SessionBean接口。并在该类中实现在第一步中编写的接口中所定义的方法。
*/
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) {}
}
RemoteHome.java
/**
* 第三步:
* 定义一个类继承javax.ejb.EJBHome 。
*/
public interface RemoteHome extends javax.ejb.EJBHome
{
RemoteInterface create()throws java.rmi.RemoteException,javax.ejb.CreateException;
}
Client.java
/**
* 第四步:
* 定义客户端类。
*/
public class Client
{
public static void main(String[] args)
{
try
{
javax.naming.Context initContext=new javax.naming.InitialContext();
Object obj=initContext.lookup("HelloWorld");
//远程查找,由名字得到对应的对象。
RemoteHome home=(RemoteHome)javax.rmi.PortableRemoteObject.narrow(obj,RemoteHome.class);
RemoteInterface remote=home.create();
String receiveFromRemote=remote.message("Hello,Remote Object!");
//远程方法调用
System.out.println("Client Received From Remote Object: /""+receiveFromRemote+"/"");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
用j2ee sdk 1.4 RI部署完成后,用C:/ HelloWorld>java -classpath %J2EE_HOME%/lib/j2ee.jar;.;HelloWorldClient.jar;
Client命令运行,出现下面的错误(捕获后用e.printStackTrace打印):
javax.naming.NoInitialContextException:Need to specify class name in environment or system property,or as an applet parameter, or in an application resource file:java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(initialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at Client.main(Client.java:10)
好象不是EJB的错误,是javax.naming的事但我该怎么改! 折腾半天还不行!!!急