GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes javax.naming.NoInitialContextException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "javax.naming.NoInitialContextException" Watch "javax.naming.NoInitialContextException" New topic
Author

javax.naming.NoInitialContextException

Aggarwal Arpit
Ranch Hand

Joined: Mar 04, 2012
Posts: 86
I am developing one simple application in Eclipse to implement JMS. when I am working with it i am getting error:

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 com.jms.Chat.main(Chat.java:35)

My Code is:

package com.jms;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.Queue;
import javax.jms.TextMessage;
import javax.naming.*;
import javax.jms.DeliveryMode;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;



public class Chat
{
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("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();
}}

please help me to solve this problem.

Thanks in Advance
Marcin Strzyz
Greenhorn

Joined: Feb 25, 2012
Posts: 5
Check if you have jndi.properties file in your classpath and appropriate entries in it, for instance it look's like there need be a value for java.naming.factory.initial property. Try to find out how this file should look like for server you are using.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1025
    
    5

Further to what Marcin has said:

http://docs.oracle.com/javase/1.4.2/docs/api/javax/naming/InitialContext.html
 
GeeCON Prague 2014
 
subject: javax.naming.NoInitialContextException