javax.naming.NoInitialContextException - Java

16

这是我的代码:

import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Sender
{
    public static void main(String[] args) throws Exception
    {
        // get the initial context
        InitialContext ctx = new InitialContext();

        // lookup the queue object
        Queue queue = (Queue) ctx.lookup("queue/queue0");

        // lookup the queue connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
           lookup("queue/connectionFactory");

        // create a queue connection
        QueueConnection queueConn = connFactory.createQueueConnection();

        // create a queue session
        QueueSession queueSession = queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);

        // create a queue sender
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        // create a simple message to say "Hello"
        TextMessage message = queueSession.createTextMessage("Hello");

        // send the message
        queueSender.send(message);

        // print what we did
        System.out.println("sent: " + message.getText());

        // close the queue connection
        queueConn.close();
    }
}

在上面的代码中,Eclipse没有报告任何错误 - 我能够成功编译。然而,当我尝试运行它时,我得到了以下异常:

Exception in thread "main" 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(Unknown Source)
  at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
  at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
  at javax.naming.InitialContext.lookup(Unknown Source)
  at Sender.main(Sender.java:21)

有人能帮我修复这个bug吗?我已经尝试修复几个小时了,但还是无法解决。


https://docs.oracle.com/javase/tutorial/jndi/ops/faq.html#1 - ROMANIA_engineer
2个回答

23

我们需要指定JNDI的INITIAL_CONTEXT_FACTORY、PROVIDER_URL、USERNAME、PASSWORD等参数来创建InitialContext

在独立应用程序中,您可以如下指定:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ldap.wiz.com:389");
env.put(Context.SECURITY_PRINCIPAL, "joeuser");
env.put(Context.SECURITY_CREDENTIALS, "joepassword");

Context ctx = new InitialContext(env);

但是如果您的代码在Java EE容器中运行,这些值将由容器获取并用于创建以下InitialContext

System.getProperty(Context.PROVIDER_URL);

并且

这些值将作为JVM参数在启动容器时设置。因此,如果您在容器中运行代码,则以下内容将适用。

InitialContext ctx = new InitialContext();

“在容器中运行代码”是什么意思? - kittu
2
据我理解,容器是指Tomcat、Jboss等。 - Toro Boro

0
如果在 EJB 客户端库上工作:
您需要提及获取初始上下文的参数。
InitialContext ctx = new InitialContext();

如果您不这样做,它将在项目文件夹中查找属性文件。同时,您也可以将属性凭据或值包含在您的类文件中,方法如下:

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL, "jnp://localhost:1099");

InitialContext ctx = new InitialContext(props);

URL_PKG_PREFIXES: 常量,保存了指定在URL上下文工厂中加载时要使用的包前缀列表的环境属性名称。

EJB客户端库是调用远程EJB组件的主要库。
这个库可以通过InitialContext来使用。为了调用EJB组件,该库通过URL上下文工厂创建了一个EJB客户端上下文。唯一必要的配置是解析java.naming.factory.url.pkgs属性的org.jboss.ejb.client.naming值,以实例化一个InitialContext。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接