Att*_*lah 61 java jndi java-ee
我正在为EJB编写客户端,在尝试执行它时,我得到以下异常:
javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或在applet参数中指定类名,或在应用程序资源文件中指定.
我只是无法理解问题所在.
ska*_*man 34
该javax.naming包包含JNDI API.因为它只是一个API,而不是一个实现,所以你需要告诉它使用哪个JNDI实现.这些实现通常特定于您尝试与之交谈的服务器.
要指定实现,请在Properties构造时传入对象InitialContext.这些属性指定要使用的实现以及服务器的位置.默认InitialContext构造函数仅在存在系统属性时才有用,但属性与手动传递它们的属性相同.
至于您需要设置哪些属性,这取决于您的服务器.您需要搜索这些设置并将其插入.
nic*_*u09 12
你应该设置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");
Run Code Online (Sandbox Code Playgroud)
我知道这是一个迟到的答案,但只是给将来参考.
sta*_*ker 11
您需要将以下名称/值对放入Hashtable并调用此构造函数:
public InitialContext(Hashtable<?,?> environment)
Run Code Online (Sandbox Code Playgroud)
确切的值取决于您的应用程序服务器,此示例适用于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
Run Code Online (Sandbox Code Playgroud)
Jua*_*nZe 10
是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 (运行时问题)
具体来说,我InitialContext在SpringBoot中尝试检索嵌入式Tomcat7实例中的默认(无参数)时遇到此问题.
我的解决方案是告诉Tomcat enableNaming.
即
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
}
Run Code Online (Sandbox Code Playgroud)
简单且可配置的解决方案是创建一个 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
Run Code Online (Sandbox Code Playgroud)
只需指定您的命名工厂和 url 并将此文件放在您的类路径中。JMS 将自行获取所需的信息,并且将来也可以轻松配置。
| 归档时间: |
|
| 查看次数: |
174222 次 |
| 最近记录: |