0

i am new for Jms. I downloaded jms tutorial from sun web site, when i coded first jms application using this tutorial,i get an exception.My code is like this. Exception is Naming Exception:

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.

My code:

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());


  }
}
nihasmata
  • 652
  • 1
  • 8
  • 28
  • Hii..check out this link http://stackoverflow.com/questions/1525385/noinitialcontextexception-errori/20042444#20042444 – nichu09 Nov 18 '13 at 07:42

1 Answers1

0

You can only use the below when you are running inside a container like web-container. It is known as the default context.

Context context=new InitialContext();

Since, your's is a java desktop application, you are supposed to specify the context factory, provider url etc. Read this article.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327