Jms异常:javax.naming.NoInitialContextException

时间:2012-08-07 13:33:45

标签: java jms jndi

我是Jms的新手。我从sun网站下载了jms教程,当我使用本教程编写第一个jms应用程序时,我得到一个例外。我的代码是这样的。例外是命名异常:

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 Exception in thread "main" java.lang.NullPointerException.

我的代码:

public class SampleClass1 {
  public static void main(String[] args) throws NamingException, JMSException {

     Context context=new InitialContext();

    QueueConnectionFactory queueConnectionFactory=(QueueConnectionFactory)context.lookup("QueueConnectionFactory");

    Queue queue=(Queue) context.lookup("MyQueue");

    QueueConnection queueConnection=(QueueConnection) queueConnectionFactory.createQueueConnection();

    QueueSession queueSession=queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
    QueueSender queueSender=queueSession.createSender(queue);

    Message message=(Message) queueSession.createTextMessage("Hello Word");
    queueSender.send((javax.jms.Message) message);

    QueueReceiver queueReceiver=queueSession.createReceiver(queue);

    queueConnection.start();

    Message receivedMessage=(Message) queueReceiver.receive();
    System.out.println(receivedMessage.toString());


  }
}

1 个答案:

答案 0 :(得分:0)

在web容器之类的容器内运行时,只能使用以下内容。它被称为default context

Context context=new InitialContext();

因为你的是一个java桌面应用程序,你应该指定上下文工厂,提供者URL等。阅读this article