Java NoInitialContextException 错误的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The meaning of NoInitialContextException error
提问by Attilah
I am writing a client for my EJB and when trying to execute it, I get the following exception :
我正在为我的 EJB 编写一个客户端,在尝试执行它时,我收到以下异常:
javax.naming.NoInitialContextException
: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file.
javax.naming.NoInitialContextException
: 需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中。
I just can't understand what the problem is.
我只是不明白问题是什么。
回答by JuanZe
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.
是 JNDI 问题。如果InitialContext
该类既没有 JNDI 服务提供者的默认属性,也没有显式配置的服务器属性,您将看到该异常。
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.
将Context.INITIAL_CONTEXT_FACTORY
环境属性设置为您正在使用的初始上下文实现的类名。此类必须可用于类路径中的程序。
Check:
查看:
回答by stacker
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
确切的值取决于您的应用程序服务器,此示例适用于 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
回答by skaffman
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.
该javax.naming
包包含 JNDI API。由于它只是一个 API,而不是一个实现,因此您需要告诉它使用 JNDI 的哪个实现。这些实现通常特定于您尝试与之通信的服务器。
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.
要指定实现,请在Properties
构造InitialContext
. 这些属性指定要使用的实现以及服务器的位置。默认InitialContext
构造函数仅在存在系统属性时才有用,但这些属性与您手动传入的属性相同。
As to which properties you need to set, that depends on your server. You need to hunt those settings down and plug them in.
至于您需要设置哪些属性,这取决于您的服务器。您需要查找这些设置并将其插入。
回答by Maarten van Leunen
Most of the time these settings are also defined in a jndi.properties
file. Do you have that one lying around somewhere?
大多数情况下,这些设置也在jndi.properties
文件中定义。你有那个躺在什么地方吗?
回答by Thom
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...
我的问题是我正在创建一个休眠会话,但由于类路径问题,我的数据库实例的 JNDI 设置错误。仅供参考...
回答by Neocy
make sure dependencies for jetty naming and jetty plus are included (not just provided scope). This fixed it for me.
确保包含 jetty 命名和 jetty plus 的依赖项(不仅仅是提供的范围)。这为我修好了。
回答by nichu09
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.
您应该设置 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");
I know this is a late answer, but just giving for future reference.
我知道这是一个迟到的答案,但仅供参考。
回答by Yannickv
I solved the same problem by adding the following Jar libraries to my project:
我通过将以下 Jar 库添加到我的项目中解决了同样的问题:
- appserv-rt.jar
- javaee.jar
- appserv-rt.jar
- javaee.jar
from the folder : C:\Program Files\glassfish-4.0\glassfish\lib
从文件夹: 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.
到这些库的链接已断开,Netbeans 未找到要使用的正确类。
回答by Vaibhav Jain
Easy & configurable solution is create one jndi.properties file and put this file in classpath. jndi.properties can be created as
简单且可配置的解决方案是创建一个 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
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.
只需指定您的命名工厂和 url 并将此文件放在您的类路径中。JMS 将自行获取所需的信息,并且将来也可以轻松配置。
回答by ACV
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
C:\installs\glassfish\glassfish-4.1\glassfish\lib\gf-client.jar
相应地调整路径