NoInitialContextException错误的含义
我正在为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
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 theInitialContext
. These properties specify the implementation to use, as well as the location of the server. The defaultInitialContext
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.
您应该设置 jndi.properties。我在下面给出了一些代码,解释了如何为 activemq 设置属性。就像你可以为你的应用程序设置一样。在像 JBoss 这样的 J2EE 容器中,不需要设置这些属性。
我知道这是一个迟到的答案,但仅供将来参考。
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.
I know this is a late answer, but just giving for future reference.
您需要将以下名称/值对放入哈希表中并调用此构造函数:
确切的值取决于您的应用程序服务器,此示例适用于 jboss
you need to put the following name/value pairs into a hash table and call this constructor:
the exact values depend on your application server, this example is for jboss
是JNDI问题。如果
InitialContext
类既没有 JNDI 服务提供程序的默认属性,也没有显式配置的服务器属性,您将看到该异常。将 Context.INITIAL_CONTEXT_FACTORY 环境属性设置为您正在使用的初始上下文实现的类名称。该类必须在类路径中可供您的程序使用。
检查:
http://docs.oracle .com/javase/7/docs/api/javax/naming/InitialContext.html
http://java.sun.com/products/jndi/tutorial/getStarted/TOC。 html(运行时问题)
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:
http://docs.oracle.com/javase/7/docs/api/javax/naming/InitialContext.html
http://java.sun.com/products/jndi/tutorial/getStarted/TOC.html (runtime problems)
具体来说,当我尝试在 SpringBoot 中的嵌入式 Tomcat7 实例中检索默认(无参数)
InitialContext
时,我遇到了这个问题。对我来说,解决方案是告诉 Tomcat
enableNaming
。IE
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.
简单&可配置的解决方案是创建一个 jndi.properties 文件并将该文件放入类路径中。
jndi.properties 可以创建为
只需指定您的命名工厂 & url 并将该文件放入您的类路径中。 JMS 将自行获取所需信息,并且将来也可以轻松配置。
Easy & configurable solution is create one jndi.properties file and put this file in classpath.
jndi.properties can be created as
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.
大多数情况下,这些设置也在
jndi.properties
文件中定义。你在某处有那个吗?Most of the time these settings are also defined in a
jndi.properties
file. Do you have that one lying around somewhere?我通过将以下 Jar 库添加到我的项目中解决了同样的问题:
来自文件夹:
C:\Program Files\glassfish-4.0\glassfish\lib
的链接这些库已损坏,Netbeans 找不到合适的类来使用。
I solved the same problem by adding the following Jar libraries to my project:
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.
我的问题是,我正在创建一个休眠会话,但由于类路径问题,我的数据库实例的 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...
您需要在客户端项目中使用 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
确保包含 jetty 命名和 jetty plus 的依赖项(不仅仅是提供的范围)。
这为我解决了这个问题。
make sure dependencies for jetty naming and jetty plus are included (not just provided scope).
This fixed it for me.
执行此操作:
同时将其添加到项目的库中:
C:\installs\glassfish\glassfish-4.1\glassfish\lib\gf-client.jar
相应地调整路径
Do this:
Also add this to the libraries of the project:
C:\installs\glassfish\glassfish-4.1\glassfish\lib\gf-client.jar
adjust path accordingly
尝试在运行时环境中添加 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