javax.naming.NoInitialContextException - Java

时间:2013-04-03 05:26:38

标签: java java-ee exception

这是我的代码:

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)

任何人都可以帮我解决这个问题吗?我试着把它固定几个小时但仍然无法弄明白。

2 个答案:

答案 0 :(得分:19)

我们需要指定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();

答案 1 :(得分: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。

相关问题