尝试访问会话Bean时出错

时间:2014-01-10 13:16:23

标签: java java-ee ejb-3.0 jndi

我在运行一个简单的EJB3客户端访问会话bean时得到javax.naming.NoInitialContextException

以下是我的客户端类:

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)

2 个答案:

答案 0 :(得分:1)

  1. 我将以下库提供给eclipse的构建路径,问题就消失了。
  2. 无论出于何种原因,将这些jar文件添加到项目中都会消除getlookup errorjavax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者在applet参数中指定类名,或者在应用程序资源文件中指定:java。 naming.factory.initial
  3. 3 JAR名称位置 jboss-transaction-api_1.1_spec-1.0.0.Final.jar AS7_HOME / modules / javax / transaction / api / main /

    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 / marshalling / main /

    xnio-api-3.0.0.CR5.jar AS7_HOME / modules / org / jboss / xnio / main /

    jboss-remoting-3.2.0.CR6.jar AS7_HOME / modules / org / jboss / remoting3 / main /

    jboss-logging-3.1.0.Beta3.jar AS7_HOME / modules / org / jboss / logging / main /

    xnio-nio-3.0.0.CR5.jar AS7_HOME / modules / org / jboss / xnio / nio / main /

    jboss-sasl-1.0.0.Beta9.jar AS7_HOME / modules / org / jboss / sasl / main /

    jboss-marshalling-river-1.3.0.GA.jar AS7_HOME / modules / org / jboss / marshalling / river / main /

答案 1 :(得分:0)

我认为您应该在应用程序客户端中运行代码,它将为您创建初始上下文。有关详细信息,请参阅Java EE Application client container