File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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


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(

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.

// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();

// create a queue session
QueueSession queueSession = queueConn.createQueueSession(false,

// create a queue sender
QueueSender queueSender = queueSession.createSender(queue);

// create a simple message to say "Hello"
TextMessage message = queueSession.createTextMessage("Hello");

// send the message

// print what we did
System.out.println("sent: " + message.getText());

// close the queue connection

please help me to solve this problem.

Thanks in Advance
Marcin Strzyz

Joined: Feb 25, 2012
Posts: 5
Check if you have 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

Joined: Nov 09, 2011
Posts: 1051

Further to what Marcin has said:
I agree. Here's the link:
subject: javax.naming.NoInitialContextException
It's not a secret anymore!