简体   繁体   English

尝试访问会话Bean时出错

[英]Error while trying to access a session bean

I am getting javax.naming.NoInitialContextException while running a simple EJB3 cient to access a session bean. 我在运行简单的EJB3来访问会话bean时遇到javax.naming.NoInitialContextException

Below is my client class: 以下是我的客户课程:

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;
    }

}

Below is the error I am getting: 以下是我遇到的错误:

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)
  1. I supplied the following libraries to the build path of eclipse and the problem went away. 我将以下库提供给Eclipse的构建路径,问题消失了。
  2. For whatever reason adding these jar files to the project gets rid of the getlookup errorjavax.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 不管出于什么原因,将这些jar文件添加到项目中都会摆脱getlookup错误javax.naming.NoInitialContextException:需要在环境或系统属性中或作为applet参数或在应用程序资源文件中指定类名:java.naming.factory 。初始

3 JAR name Location jboss-transaction-api_1.1_spec-1.0.0.Final.jar AS7_HOME/modules/javax/transaction/api/main/ 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-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-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/ 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/ 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-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/ 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/ 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-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/ jboss-marshalling-river-1.3.0.GA.jar AS7_HOME / modules / org / jboss / marshalling / river / main /

I think you should run your code inside an application client, and it will create the initial context for you. 我认为您应该在应用程序客户端中运行代码,它将为您创建初始上下文。 See Java EE Application client container for more details. 有关更多详细信息,请参见Java EE Application客户端容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在会话bean中使用EntityManager时出错 - error while using EntityManager in session bean 在 spring 中将会话范围应用于 bean 时出错 - Error while applying session scope to a bean in spring JSTL在尝试访问bean时给出错误 - JSTL giving Error when trying to access bean 尝试访问dao bean时自动连接依赖项的注入失败 - Injection of autowired dependencies failed while trying to access dao bean EJB-对有状态会话Bean执行JNDI查找时出现序列化错误 - EJB - Serialization error while doing a JNDI lookup for a Stateful Session Bean 尝试在托管bean中连接到Cassandra时出现番石榴错误 - Getting a guava error while trying to connect to Cassandra in a managed bean 尝试访问不安全的路由时出现 Gettig 错误 - Gettig error while trying to access unsecured route 从另一个EAR访问本地会话Bean? - access a Local Session Bean from another EAR? 有状态会话bean多线程访问 - Stateful session bean multi-threaded access 尝试自动连接使用MongoDB使用Morphia的类时出现Spring bean配置错误 - Error with spring bean configuraion while trying to autowire class that usind Morphia with MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM