EJB 部署及應用

myz001發表於2004-01-29
我編寫了一個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>

// 在此輸入java程式碼
<p class="indent">

相關文章