2013-05-02 38 views
0

我正在執行Head First EJB。我知道它具有與EJB 3.1相同的舊版本的EJB 2.0,但我認爲Head是第一本爲學習像我們這樣的業餘愛好者準備的好書。錯誤執行Head First EJB第一個示例

我使用J2ee 1.3.1 RI和jdk 1.4。我運行了「部署」工具來部署EJB,現在我有一個ejb客戶端JAVA程序給出如下:

import javax.naming.*; 
import java.rmi.*; 
import javax.rmi.*; 
import headfirst.*; 
import javax.ejb.*; 

import java.util.*; 

public class AdviceClient { 
    public static void main(String[] args){ 
     new AdviceClient().go(); 
    } 

    public void go(){ 
     try{ 

      Properties props=new Properties(); 
      props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); 
      //props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); 
      //env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); 
      //props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory"); 
      props.setProperty(Context.PROVIDER_URL,"rmi://localhost:1050"); 
      //props.setProperty("java.naming.provider.url","rmi://localhost:1099"); 

      Context ic = new InitialContext(props); 
      Object o = ic.lookup("Advisor"); 

      AdviceHome home = (AdviceHome)PortableRemoteObject.narrow(o,AdviceHome.class); 

      Advice advisor = home.create(); 
      System.out.println(advisor.getAdvice()); 
     } 
     catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 
} 






Following the book, I compiled the program. The "Properties" part is something not mentioned in the example. I got it from some other examples on the web. Now, that when I am executing the client, am getting the following error: 


F:\EJBProject\advice>java -classpath AdviceAppClient.jar;. AdviceClient 
javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 

     java.net.SocketTimeoutException: Read timed out] 
     at com.sun.jndi.rmi.registry.RegistryContext.lookup(Unknown Source) 
     at com.sun.jndi.rmi.registry.RegistryContext.lookup(Unknown Source) 
     at javax.naming.InitialContext.lookup(Unknown Source) 
     at AdviceClient.go(AdviceClient.java:26) 
     at AdviceClient.main(AdviceClient.java:11) 
Caused by: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
     java.net.SocketTimeoutException: Read timed out 
     at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source) 
     at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source) 
     at sun.rmi.server.UnicastRef.newCall(Unknown Source) 
     at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) 
     ... 5 more 
Caused by: java.net.SocketTimeoutException: Read timed out 
     at java.net.SocketInputStream.socketRead0(Native Method) 
     at java.net.SocketInputStream.read(Unknown Source) 
     at java.net.SocketInputStream.read(Unknown Source) 
     at java.io.BufferedInputStream.fill(Unknown Source) 
     at java.io.BufferedInputStream.read(Unknown Source) 
     at java.io.DataInputStream.readByte(Unknown Source) 

請幫忙。我在這裏困住了。我想參加OCBCD考試!

問候, Shouvanik

+0

您的錯誤表明您嘗試連接的RMI服務器不存在。 RMI服務器在哪個端口監聽?你使用的是什麼j2ee服務器?那是太陽j2ee服務器嗎? – 2013-05-02 03:45:11

+0

RMI端口是1050,它正在運行。我使用j2ee 1.3 RI和jdk1.4。 – 2013-05-02 04:42:59

+0

從獨立的Java程序連接到rmi服務器時,請檢查所有這些必需的庫和說明。它也有樣本程序。 >> http://docs.oracle.com/cd/E19644-01/817-5050/dcacc.html#wp1016235 – 2013-05-02 10:40:46

回答

0

終於解決了。

如果您在運行第一個EJB示例時遇到了障礙,只需按照以下步驟 運行HeadFirst EJB的第一個示例。

軟件 1. J2SDK1.4.1 - JDK 2. J2SDKEE1.3.1 - J2EE RI 3. CMD(命令提示)

編譯所有用c類:\ J2SDKEE1.3.1 \ lib中\ J2EE。 jar庫。 現在,創建一個應用程序並在RI內部署EJB。按照本書所示下載應用程序客戶端。

現在,來了棘手的部分。

使用以下罐子

javac的編譯客戶端類-classpath AdviceAppClient.jar; C:\ J2SDKEE1.3.1 \ lib中\的j2ee.jar AdviceClient.java

接下來,運行在客戶端如下

java -cp AdviceAppClient.jar; c:\ j2sdkee1.3.1 \ lib \ j2ee.jar; c:\ j2sdk1.4.1 \ jre \ lib \ rt.jar ;. AdviceClient