JBossEJB客户端示例:线程中的异常;“主要”;javax.naming.NoInitialContextException:

JBossEJB客户端示例:线程中的异常;“主要”;javax.naming.NoInitialContextException:,java,eclipse,jakarta-ee,jboss,ejb-3.0,Java,Eclipse,Jakarta Ee,Jboss,Ejb 3.0,我在eclipse中尝试运行下面的示例 我已经创建了一个ejb项目,并将所有类文件和.properties文件导入到META-INF中。现在代码中没有错误,ejb已成功部署到JBoss服务器上。当我尝试运行RemoteEJBClient.java时,我遇到了以下异常,我无法修复 Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment

我在eclipse中尝试运行下面的示例

我已经创建了一个ejb项目,并将所有类文件和.properties文件导入到META-INF中。现在代码中没有错误,ejb已成功部署到JBoss服务器上。当我尝试运行RemoteEJBClient.java时,我遇到了以下异常,我无法修复

Exception in thread "main" 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(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.jboss.as.quickstarts.ejb.remote.client.RemoteEJBClient.lookupRemoteStatelessCalculator(RemoteEJBClient.java:131)
    at org.jboss.as.quickstarts.ejb.remote.client.RemoteEJBClient.invokeStatelessBean(RemoteEJBClient.java:50)
    at org.jboss.as.quickstarts.ejb.remote.client.RemoteEJBClient.main(RemoteEJBClient.java:37)
请帮我解决这个问题

主要独立类:

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.as.quickstarts.ejb.remote.client;

import org.jboss.as.quickstarts.ejb.remote.stateful.RemoteCounter;
import org.jboss.as.quickstarts.ejb.remote.stateless.RemoteCalculator;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

/**
 * A sample program which acts a remote client for a EJB deployed on AS7 server.
 * This program shows how to lookup stateful and stateless beans via JNDI and
 * then invoke on them
 * 
 * @author Jaikiran Pai
 */
public class RemoteEJBClient {

    public static void main(String[] args) throws Exception {
        // Invoke a stateless bean
        invokeStatelessBean();

        // Invoke a stateful bean
        // invokeStatefulBean();
    }

    /**
     * Looks up a stateless bean and invokes on it
     * 
     * @throws NamingException
     */
    private static void invokeStatelessBean() throws NamingException {
        // Let's lookup the remote stateless calculator
        final RemoteCalculator statelessRemoteCalculator = lookupRemoteStatelessCalculator();
        System.out
                .println("Obtained a remote stateless calculator for invocation");
        // invoke on the remote calculator
        int a = 204;
        int b = 340;
        System.out
                .println("Adding "
                        + a
                        + " and "
                        + b
                        + " via the remote stateless calculator deployed on the server");
        int sum = statelessRemoteCalculator.add(a, b);
        System.out.println("Remote calculator returned sum = " + sum);
        if (sum != a + b) {
            throw new RuntimeException(
                    "Remote stateless calculator returned an incorrect sum "
                            + sum + " ,expected sum was " + (a + b));
        }
        // try one more invocation, this time for subtraction
        int num1 = 3434;
        int num2 = 2332;
        System.out
                .println("Subtracting "
                        + num2
                        + " from "
                        + num1
                        + " via the remote stateless calculator deployed on the server");
        int difference = statelessRemoteCalculator.subtract(num1, num2);
        System.out.println("Remote calculator returned difference = "
                + difference);
        if (difference != num1 - num2) {
            throw new RuntimeException(
                    "Remote stateless calculator returned an incorrect difference "
                            + difference + " ,expected difference was "
                            + (num1 - num2));
        }
    }

    /**
     * Looks up a stateful bean and invokes on it
     * 
     * @throws NamingException
     */
    private static void invokeStatefulBean() throws NamingException {
        // Let's lookup the remote stateful counter
        final RemoteCounter statefulRemoteCounter = lookupRemoteStatefulCounter();
        System.out.println("Obtained a remote stateful counter for invocation");
        // invoke on the remote counter bean
        final int NUM_TIMES = 5;
        System.out.println("Counter will now be incremented " + NUM_TIMES
                + " times");
        for (int i = 0; i < NUM_TIMES; i++) {
            System.out.println("Incrementing counter");
            statefulRemoteCounter.increment();
            System.out.println("Count after increment is "
                    + statefulRemoteCounter.getCount());
        }
        // now decrementing
        System.out.println("Counter will now be decremented " + NUM_TIMES
                + " times");
        for (int i = NUM_TIMES; i > 0; i--) {
            System.out.println("Decrementing counter");
            statefulRemoteCounter.decrement();
            System.out.println("Count after decrement is "
                    + statefulRemoteCounter.getCount());
        }
    }

    /**
     * Looks up and returns the proxy to remote stateless calculator bean
     * 
     * @return
     * @throws NamingException
     */
    private static RemoteCalculator lookupRemoteStatelessCalculator()
            throws NamingException {
        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES,
                "org.jboss.ejb.client.naming");

        /*
         * remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=
         * false
         * 
         * remote.connections=default
         * 
         * remote.connection.default.host=localhost
         * remote.connection.default.port = 4447
         * remote.connection.default.connect
         * .options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
         */
        jndiProperties
                .put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED",
                        false);
        jndiProperties.put("remote.connections", "default");
        jndiProperties.put("remote.connection.default.host", "localhost");
        jndiProperties.put("remote.connection.default.port", "4447");
        jndiProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS",false);

        final Context context = new InitialContext(jndiProperties);

        // The JNDI lookup name for a stateless session bean has the syntax of:
        // ejb:<appName>/<moduleName>/<distinctName>/<beanName>!<viewClassName>
        //
        // <appName> The application name is the name of the EAR that the EJB is
        // deployed in
        // (without the .ear). If the EJB JAR is not deployed in an EAR then
        // this is
        // blank. The app name can also be specified in the EAR's
        // application.xml
        //
        // <moduleName> By the default the module name is the name of the EJB
        // JAR file (without the
        // .jar suffix). The module name might be overridden in the ejb-jar.xml
        //
        // <distinctName> : AS7 allows each deployment to have an (optional)
        // distinct name.
        // This example does not use this so leave it blank.
        //
        // <beanName> : The name of the session been to be invoked.
        //
        // <viewClassName>: The fully qualified classname of the remote
        // interface. Must include
        // the whole package name.

        // let's do the lookup
        return (RemoteCalculator) context
                .lookup("java:global/ejb-remote/CalculatorBean!org.jboss.as.quickstarts.ejb.remote.stateless.RemoteCalculator");
    }

    /**
     * Looks up and returns the proxy to remote stateful counter bean
     * 
     * @return
     * @throws NamingException
     */
    private static RemoteCounter lookupRemoteStatefulCounter()
            throws NamingException {
        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES,
                "org.jboss.ejb.client.naming");
        final Context context = new InitialContext(jndiProperties);

        // The JNDI lookup name for a stateful session bean has the syntax of:
        // ejb:<appName>/<moduleName>/<distinctName>/<beanName>!<viewClassName>?stateful
        //
        // <appName> The application name is the name of the EAR that the EJB is
        // deployed in
        // (without the .ear). If the EJB JAR is not deployed in an EAR then
        // this is
        // blank. The app name can also be specified in the EAR's
        // application.xml
        //
        // <moduleName> By the default the module name is the name of the EJB
        // JAR file (without the
        // .jar suffix). The module name might be overridden in the ejb-jar.xml
        //
        // <distinctName> : AS7 allows each deployment to have an (optional)
        // distinct name.
        // This example does not use this so leave it blank.
        //
        // <beanName> : The name of the session been to be invoked.
        //
        // <viewClassName>: The fully qualified classname of the remote
        // interface. Must include
        // the whole package name.

        // let's do the lookup
        return (RemoteCounter) context
                .lookup("ejb:/jboss-ejb-remote-server-side/CounterBean!"
                        + RemoteCounter.class.getName() + "?stateful");
    }
}
运行pom.xml也充满了错误。这可能意味着jboss提供的pom.xml也无法工作

[错误]不可解析的导入POM:在中找不到org.jboss.spec:jboss-javaee-6.0:POM:3.0.2.Final-redhat-4。 org/maven2缓存在本地存储库中,在经过central的更新间隔或更新之前,不会重新尝试解析 在第72行第22列->处强制执行[Help 2] [错误]无法解析的导入POM:在中找不到org.jboss.as:jboss as ejb客户端bom:POM:7.2.1.Final-redhat-10 pache.org/maven2缓存在本地存储库中,在经过central的更新间隔或 第80行第22列强制更新->[帮助2] [错误].jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar的“dependencies.dependency.version”丢失。@第94行, 第19栏 [错误]:缺少org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:jar的“dependencies.dependency.version”。@第102行第19列 [错误].jboss:jboss ejb客户端:jar的“dependencies.dependency.version”丢失。@第118行第20列 [错误].jboss.xnio:xnio api:jar的“dependencies.dependency.version”丢失。@第125行第20列 [错误].jboss.xnio:xnio-nio:jar的“dependencies.dependency.version”丢失。@第131行第20列 [错误]:org.jboss.remoting3:jboss remoting:jar的“dependencies.dependency.version”丢失。@第138行第20列 [错误]:org.jboss.sasl:jboss sasl:jar的“dependencies.dependency.version”丢失。@第145行第21栏 [错误]:org.jboss.marshalling:jboss marshalling river:jar的“dependencies.dependency.version”丢失。@第152行第21列 [错误] [错误]要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。 [错误]使用-X开关重新运行Maven以启用完整调试日志记录。 [错误] [错误]有关错误和可能的解决方案的更多信息,请阅读以下文章: [错误][帮助1]
[错误][帮助2]

您的EJB客户端是java独立客户端,因此,为了连接到JNDI服务并查找EJB引用,您需要为InitialContext对象提供一些配置

异常告诉您没有提供此配置,因此无法创建InitialContext实例

您可以了解(一般而言)如何设置InitialConText属性。请记住,此配置取决于供应商


在本教程中搜索,我可以看到一个似乎包含适合本教程的属性的。我似乎觉得这个文件对RemoteEJBClient.java类不可用

谢谢。我试过两种方法,一种是复制.properites文件,即主Cals是什么,但它没有起作用。然后我在主类initialcontext中设置了这些值,但仍然得到相同的错误。有什么想法吗?你的jboss版本和教程相同吗(或者至少相似)?我正在运行这个版本:jboss-as-7.1.1.Finalfrom尝试设置这个属性:endpoint.name=client endpoint我已经添加了这个代码,它仍然返回相同的异常
#
# JBoss, Home of Professional Open Source
# Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
# contributors by the @authors tag. See the copyright.txt in the
# distribution for a full listing of individual contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false