NoInitialContextException错误的含义

发布于 2024-08-07 01:15:24 字数 194 浏览 5 评论 0原文

我正在为我的 EJB 编写一个客户端,当尝试执行它时,出现以下异常:

javax.naming.NoInitialContextException:需要指定类名 在环境或系统属性中,或作为小程序参数,或在 应用程序资源文件。

我只是不明白问题是什么。

I am writing a client for my EJB and when trying to execute it, I get the following exception :

javax.naming.NoInitialContextException: Need to specify class name
in environment or system property, or as an applet parameter, or in an
application resource file.

I just can't understand what the problem is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

滴情不沾 2024-08-14 01:15:24

javax.naming 包包含 JNDI API。由于它只是一个 API,而不是实现,因此您需要告诉它要使用哪个 JNDI 实现。这些实现通常特定于您尝试与之通信的服务器。

要指定实现,请在构造 InitialContext 时传入 Properties 对象。这些属性指定要使用的实现以及服务器的位置。默认的 InitialContext 构造函数仅在存在系统属性时有用,但这些属性与手动传递它们时相同。

至于需要设置哪些属性,这取决于您的服务器。您需要找到这些设置并将其插入。

The javax.naming package comprises the JNDI API. Since it's just an API, rather than an implementation, you need to tell it which implementation of JNDI to use. The implementations are typically specific to the server you're trying to talk to.

To specify an implementation, you pass in a Properties object when you construct the InitialContext. These properties specify the implementation to use, as well as the location of the server. The default InitialContext constructor is only useful when there are system properties present, but the properties are the same as if you passed them in manually.

As to which properties you need to set, that depends on your server. You need to hunt those settings down and plug them in.

怪我闹别瞎闹 2024-08-14 01:15:24

您应该设置 jndi.properties。我在下面给出了一些代码,解释了如何为 activemq 设置属性。就像你可以为你的应用程序设置一样。在像 JBoss 这样的 J2EE 容器中,不需要设置这些属性。

Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
InitialContext ctx = new InitialContext(props);
// get the initial context
// InitialContext ctx = new InitialContext();
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");        
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();   
queueConn.start();
// lookup the queue object
Queue queue = (Queue) ctx.lookup("dynamicQueues/Payment_Check");  

我知道这是一个迟到的答案,但仅供将来参考。

You should set jndi.properties. I've given below some piece of code that explain how the properties are set for activemq. Like that you can set for your application. Inside a J2EE container like JBoss no need to set these properties.

Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
InitialContext ctx = new InitialContext(props);
// get the initial context
// InitialContext ctx = new InitialContext();
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");        
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();   
queueConn.start();
// lookup the queue object
Queue queue = (Queue) ctx.lookup("dynamicQueues/Payment_Check");  

I know this is a late answer, but just giving for future reference.

烟火散人牵绊 2024-08-14 01:15:24

您需要将以下名称/值对放入哈希表中并调用此构造函数:

public InitialContext(Hashtable<?,?> environment)

确切的值取决于您的应用程序服务器,此示例适用于 jboss

jndi.java.naming.provider.url=jnp://localhost:1099/
jndi.java.naming.factory.url=org.jboss.naming:org.jnp.interfaces
jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

you need to put the following name/value pairs into a hash table and call this constructor:

public InitialContext(Hashtable<?,?> environment)

the exact values depend on your application server, this example is for jboss

jndi.java.naming.provider.url=jnp://localhost:1099/
jndi.java.naming.factory.url=org.jboss.naming:org.jnp.interfaces
jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
相思碎 2024-08-14 01:15:24

是JNDI问题。如果 InitialContext 类既没有 JNDI 服务提供程序的默认属性,也没有显式配置的服务器属性,您将看到该异常。

将 Context.INITIAL_CONTEXT_FACTORY 环境属性设置为您正在使用的初始上下文实现的类名称。该类必须在类路径中可供您的程序使用。

检查:

Is a JNDI problem. You will see that exception if the InitialContext class has neither default properties for the JNDI service provider nor explicitly configured server properties.

Set the Context.INITIAL_CONTEXT_FACTORY environment property to the class name of the initial context implementation that you are using. This class must be available to your program in the classpath.

Check:

糖果控 2024-08-14 01:15:24

具体来说,当我尝试在 SpringBoot 中的嵌入式 Tomcat7 实例中检索默认(无参数)InitialContext 时,我遇到了这个问题。

对我来说,解决方案是告诉 Tomcat enableNaming

IE

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}

Specifically, I got this issue when attempting to retrieve the default (no-args) InitialContext within an embedded Tomcat7 instance, in SpringBoot.

The solution for me, was to tell Tomcat to enableNaming.

i.e.

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}
↘人皮目录ツ 2024-08-14 01:15:24

简单&可配置的解决方案是创建一个 jndi.properties 文件并将该文件放入类路径中。
jndi.properties 可以创建为

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector
java.naming.provider.url = vm://localhost

# use the following property to specify the JNDI name the connection factory
# should appear as. 
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue


# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic

只需指定您的命名工厂 & url 并将该文件放入您的类路径中。 JMS 将自行获取所需信息,并且将来也可以轻松配置。

Easy & configurable solution is create one jndi.properties file and put this file in classpath.
jndi.properties can be created as

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector
java.naming.provider.url = vm://localhost

# use the following property to specify the JNDI name the connection factory
# should appear as. 
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue


# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic

Just specify your naming factory & url and put this file in your classpath. JMS will fetch required info by itself and it's easily configurable in future also.

久而酒知 2024-08-14 01:15:24

大多数情况下,这些设置也在 jndi.properties 文件中定义。你在某处有那个吗?

Most of the time these settings are also defined in a jndi.properties file. Do you have that one lying around somewhere?

违心° 2024-08-14 01:15:24

我通过将以下 Jar 库添加到我的项目中解决了同样的问题:

  • appserv-rt.jar
  • javaee.jar

来自文件夹: C:\Program Files\glassfish-4.0\glassfish\lib

的链接这些库已损坏,Netbeans 找不到合适的类来使用。

I solved the same problem by adding the following Jar libraries to my project:

  • appserv-rt.jar
  • javaee.jar

from the folder : C:\Program Files\glassfish-4.0\glassfish\lib

The links to these libraries were broken and Netbeans didn't found the right classes to use.

葮薆情 2024-08-14 01:15:24

我的问题是,我正在创建一个休眠会话,但由于类路径问题,我的数据库实例的 JNDI 设置错误。仅供参考...

My problem with this one was that I was creating a hibernate session, but had the JNDI settings for my database instance wrong because of a classpath problem. Just FYI...

刘备忘录 2024-08-14 01:15:24

您需要在客户端项目中使用 jboss-client.jar 并且需要在 ejb 项目中使用 jnp-client jar

you need to use jboss-client.jar in your client project and you need to use jnp-client jar in your ejb project

沉睡月亮 2024-08-14 01:15:24

确保包含 jetty 命名和 jetty plus 的依赖项(不仅仅是提供的范围)。
这为我解决了这个问题。

make sure dependencies for jetty naming and jetty plus are included (not just provided scope).
This fixed it for me.

喵星人汪星人 2024-08-14 01:15:24

执行此操作:

Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
Context initialContext = new InitialContext(props);

同时将其添加到项目的库中:

C:\installs\glassfish\glassfish-4.1\glassfish\lib\gf-client.jar
相应地调整路径

Do this:

Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
Context initialContext = new InitialContext(props);

Also add this to the libraries of the project:

C:\installs\glassfish\glassfish-4.1\glassfish\lib\gf-client.jar
adjust path accordingly

童话 2024-08-14 01:15:24

尝试在运行时环境中添加 rmi java 模块:
--add-modules jdk.naming.rmi,java.base

Try adding rmi java modules in you runtime environment :
--add-modules jdk.naming.rmi,java.base

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文