“javax.naming.NoInitialContextException”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6387238/
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
What does "javax.naming.NoInitialContextException" mean?
提问by jdbcnewbie.
As the title suggests, what does "javax.naming.NoInitialContextException" mean in non technical terms? And what are some general suggestions to fix it?
正如标题所暗示的,“javax.naming.NoInitialContextException”在非技术术语中是什么意思?有什么一般性建议可以解决它?
EDIT (From the console):
编辑(从控制台):
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:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at cepars.app.ConnectionHelper.getConnection(ConnectionHelper.java:25)
at cepars.app.ConnectionHelper.getConnection(ConnectionHelper.java:10)
at cepars.review.ReviewDAO.getJobList(ReviewDAO.java:30)
at cepars.review.Test.main(Test.java:43)
java.lang.NullPointerException
at cepars.review.ReviewDAO.getJobList(ReviewDAO.java:31)
at cepars.review.Test.main(Test.java:43)
cepars.app.DAOException
at cepars.review.ReviewDAO.getJobList(ReviewDAO.java:39)
at cepars.review.Test.main(Test.java:43)
采纳答案by Andrzej Doyle
It basically means that the application wants to perform some "naming operations" (e.g. JNDI or LDAP lookups), and it didn't have sufficient information available to be able to create a connection to the directory server. As the docs for the exception state,
这基本上意味着应用程序想要执行一些“命名操作”(例如 JNDI 或 LDAP 查找),并且它没有足够的可用信息来创建到目录服务器的连接。作为异常状态的文档,
This exception is thrown when no initial context implementation can be created. The policy of how an initial context implementation is selected is described in the documentation of the InitialContext class.
当无法创建初始上下文实现时,将抛出此异常。InitialContext 类的文档中描述了如何选择初始上下文实现的策略。
And if you dutifully have a look at the javadocs for InitialContext, they describe quite well how the initial context is constructed, and what your options are for supplying the address/credentials/etc.
如果您尽职尽责地查看InitialContext的 javadocs ,它们很好地描述了初始上下文的构造方式,以及您提供地址/凭据/等的选项。
If you have a go at creating the context and get stuck somewhere else, please post back explaining what you've done so far and where you're running aground.
如果您尝试创建上下文并在其他地方卡住,请回帖解释您到目前为止所做的工作以及您搁浅的地方。
回答by Sean Patrick Floyd
Just read the docs:
只需阅读文档:
This exception is thrown when no initial context implementation can be created. The policy of how an initial context implementation is selected is described in the documentation of the InitialContext class.
This exception can be thrown during any interaction with the InitialContext, not only when the InitialContext is constructed. For example, the implementation of the initial context might lazily retrieve the context only when actual methods are invoked on it. The application should not have any dependency on when the existence of an initial context is determined.
当无法创建初始上下文实现时,将抛出此异常。InitialContext 类的文档中描述了如何选择初始上下文实现的策略。
在与 InitialContext 的任何交互期间都可以抛出此异常,而不仅仅是在构造 InitialContext 时。例如,初始上下文的实现可能仅在对其调用实际方法时才懒惰地检索上下文。应用程序不应依赖于何时确定初始上下文的存在。
But this is explained much better in the docs for InitialContext
但这在文档中解释得更好 InitialContext
回答by pap
It means that there is no initial context :)
这意味着没有初始上下文:)
But seriously folks, JNDI (javax.naming) is all about looking up objects or resources from some directory or provider. To look something up, you need somewhere to look (this is the InitialContext). NoInitialContextException means "I want to find the telephone number for John Smith, but I have no phonebook to look in".
但是认真的人,JNDI (javax.naming) 就是从某个目录或提供者中查找对象或资源。要查找某些内容,您需要在某个地方查找(这是 InitialContext)。NoInitialContextException 表示“我想找到 John Smith 的电话号码,但我没有电话簿可以查看”。
An InitialContext can be created in any number of ways. It can be done manually, for instance creating a connection to an LDAP server. It can also be set up by an application server inside which you run your application. In this case, the container (application server) already provides you with a "phonebook", through which you can look up anything the application server makes available. This is often configurable and a common way of moving this type of configuration from the application implementation to the container, where it can be shared across all applications in the server.
可以通过多种方式创建 InitialContext。它可以手动完成,例如创建到 LDAP 服务器的连接。它也可以由您在其中运行应用程序的应用程序服务器设置。在这种情况下,容器(应用程序服务器)已经为您提供了一个“电话簿”,您可以通过它查找应用程序服务器提供的任何内容。这通常是可配置的,并且是将这种类型的配置从应用程序实现移动到容器的常用方法,可以在服务器中的所有应用程序之间共享。
UPDATE: from the code snippet you post it looks like you are trying to run code stand-alone that is meant to be run in an application server. In this case, the code attempting to get a connection to a database from the "phonebook". This is one of the resources that is often configured in the application server container. So, rather than having to manage configuration and connections to the database in your code, you can configure it in your application server and simple ask for a connection (using JNDI) in your code.
更新:从您发布的代码片段来看,您似乎正在尝试独立运行要在应用程序服务器中运行的代码。在这种情况下,代码试图从“电话簿”获取到数据库的连接。这是应用服务器容器中经常配置的资源之一。因此,不必在代码中管理配置和与数据库的连接,您可以在应用程序服务器中配置它并在代码中简单地请求连接(使用 JNDI)。
回答by Trollfar
In extremely non-technical terms, it may mean that you forgot to put "ejb:" or "jdbc:" or something at the very beginning of the URI you are trying to connect.
用非常非技术性的术语来说,这可能意味着您忘记在尝试连接的 URI 的开头放置“ejb:”或“jdbc:”或其他内容。