我是 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());
}
}