2014-01-10 91 views
1

我在運行簡單的EJB3 cient訪問會話bean時收到javax.naming.NoInitialContextException嘗試訪問會話bean時出錯

下面是我的客戶端類:

package com.ibytecode.client; 
import javax.naming.Context; 
import javax.naming.NamingException; 

import com.ibytecode.business.HelloWorld; 
import com.ibytecode.businesslogic.HelloWorldBean; 
import com.ibytecode.clientutility.ClientUtility; 

public class EJBApplicationClient { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     HelloWorld bean = doLookup(); 
     System.out.println(bean.sayHello()); // 4. Call business logic 
    } 

    private static HelloWorld doLookup() { 
     Context context = null; 
     HelloWorld bean = null; 
     try { 
      // 1. Obtaining Context 
      context = ClientUtility.getInitialContext(); 
      // 2. Generate JNDI Lookup name 
      String lookupName = getLookupName(); 
      // 3. Lookup and cast 
      bean = (HelloWorld) context.lookup(lookupName); 

     } catch (NamingException e) { 
      e.printStackTrace(); 
     } 
     return bean; 
    } 

    private static String getLookupName() { 
/* 
The app name is the EAR name of the deployed EJB without .ear suffix. 
Since we haven't deployed the application as a .ear, 
the app name for us will be an empty string 
*/ 
     String appName = ""; 

     /* The module name is the JAR name of the deployed EJB 
     without the .jar suffix. 
     */ 
     String moduleName = "HelloWorldSessionBean"; 

/*AS7 allows each deployment to have an (optional) distinct name. 
This can be an empty string if distinct name is not specified. 
*/ 
     String distinctName = ""; 

     // The EJB bean implementation class name 
     String beanName = HelloWorldBean.class.getSimpleName(); 

     // Fully qualified remote interface name 
     final String interfaceName = HelloWorld.class.getName(); 

     // Create a look up string name 
     String name = "ejb:" + appName + "/" + moduleName + "/" + 
      distinctName + "/" + beanName + "!" + interfaceName; 

     return name; 
    } 

} 

下面是我得到的錯誤:

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:662) 
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) 
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344) 
at javax.naming.InitialContext.lookup(InitialContext.java:411) 
at com.ibytecode.client.EJBApplicationClient.doLookup(EJBApplicationClient.java:28) 
at com.ibytecode.client.EJBApplicationClient.main(EJBApplicationClient.java:15) 
Exception in thread "main" java.lang.NullPointerException 
at com.ibytecode.client.EJBApplicationClient.main(EJBApplicationClient.java:16) 
+0

你是如何開始你的應用程序?你是將它作爲獨立的Java應用程序還是在應用程序客戶端中運行? – marcus

+0

嗨馬庫斯, 感謝您的時間。我作爲一個獨立的Java應用程序運行它。 –

+0

你使用什麼服務器?顯示你的ClientUtility.getInitialContext()。我猜這是需要添加'props.put(Context.URL_PKG_PREFIXES,「org.jboss.ejb.client.naming」);' – Qwertovsky

回答

1
  1. 我提供了下列庫的Eclipse的構建路徑和問題走了。
  2. 無論出於什麼原因將這些jar文件添加到項目中,都會擺脫getlookup errorjavax.naming.NoInitialContextException:需要在環境或系統屬性中,或者作爲applet參數或應用程序資源文件中指定類名:java。 naming.factory.initial

3 JAR名稱位置 JBoss的事務-api_1.1_spec-1.0.0.Final.jar AS7_HOME /模塊/的javax /交易/ API /主/

的JBoss的EJB -api_3.1_spec-1.0.1.Final.jar AS7_HOME/modules/javax/ejb/api/main/

jboss-ejb-client-1.0.0.Beta10.jar AS7_HOME/modules/org/jboss/ejb-client/main/

jboss-marshalling-1.3.0.GA.jar AS7_HOME/modules/org/jboss /編組/主/

xnio-API-3.0.0.CR5.jar AS7_HOME /模塊/組織/ JBoss的/ xnio /主/

的jboss-遠程-3.2.0.CR6.jar AS7_HOME /模塊/組織/ JBoss的/ remoting3 /主/

的jboss-測井3.1.0.Beta3.jar AS7_HOME /模塊/組織/ JBoss的/記錄/主/

xnio-NIO-3.0.0.CR5。罐子AS7_H OME /模塊/組織/ JBoss的/ xnio/NIO /主/

的jboss-SASL-1.0.0.Beta9.jar AS7_HOME /模塊/組織/ JBoss的/ SASL /主/

JBoss的編組河-1.3.0.GA.jar AS7_HOME/modules/org/jboss/marshalling/river/main/

+0

基於jboss的發佈,最終的文件名可以不同,例如在我的情況下使用集成eap,在eclipse中,第一個庫的路徑是:E:\ vpa \ jboss \ jboss-eap-6.1.0 \ jboss-eap-6.1 \ modules \ system \ layers \ base \ javax \ ejb \ api \主\ jboss的-EJB-api_3.1_spec-1.0.2.Final-的redhat-2.jar – user3191424

相關問題