EJB 部署及应用
我编写了一个EJB 如下:
部署成功,但在运行时出现错误。
请各位高手不吝赐教,不胜感激!!!
运行环境、代码、命令和错误信息全部列在下面
RemoteInterface.java
/**
* 第一步:
*
定义一个新的接口继承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();
}
}
}
//:Deployment Descriptor: ejb-jar.xml
<?xml version='1.0' encoding='UTF-8'?> <ejb-jar version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" > <display-name xml:lang="zh" >HelloWorld</display-name> <enterprise-beans> <session> <ejb-name>RemoteObject</ejb-name> <home>RemoteHome</home> <remote>RemoteInterface</remote> <ejb-class>RemoteObject</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> <ejb-ref> <ejb-ref-name>HelloWorld</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>RemoteHome</home> <remote>RemoteInterface</remote> </ejb-ref> <security-identity> <use-caller-identity> </use-caller-identity> </security-identity> </session> </enterprise-beans> </ejb-jar>
运行环境:
indows XP Professional
Sun MicroSystem j2ee 1.4
Deployment Tool for Java 3 Platform Enterprise Edition 1.4
部署成功!
发出命令:
D:\HelloWorld>java -classpath c:\sun\appserver\lib\j2ee.jar;c:\sun\appserver\lib;.;HelloWorldClient.jar; Client
错误信息:
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:12)
D:\HelloWorld>
|