I am quite new with the JMS. I am trying to have a simple program running in order to start from it towards the application I want to build.

I tried to run the sample program below:

package chat;

import javax.jms.*;
import javax.naming.*;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Properties;

public class Chat implements javax.jms.MessageListener {
private TopicSession pubSession;
private TopicSession subSession;
private TopicPublisher publisher;
private TopicConnection connection;

/* Constructor. Establish JMS publisher and subscriber */
public Chat() throws Exception {
// Obtain a JNDI connection
Properties env = new Properties();
// ... specify the JNDI properties specific to the vendor

InitialContext jndi = new InitialContext(env);

// Look up a JMS connection factory
TopicConnectionFactory conFactory =
(TopicConnectionFactory) jndi.lookup("TopicConnectionFactory");

// Create a JMS connection
TopicConnection connection =

// Create two JMS session objects
TopicSession pubSession =
TopicSession subSession =

// Look up a JMS topic
Topic chatTopic = (Topic) jndi.lookup("anytopic");

// Create a JMS publisher and subscriber
TopicPublisher publisher =
TopicSubscriber subscriber =

// Set a JMS message listener

// Intialize the Chat application
set(connection, pubSession, subSession, publisher);

// Start the JMS connection; allows messages to be delivered


/* Initialize the instance variables */
public void set(TopicConnection con, TopicSession pubSess,
TopicSession subSess, TopicPublisher pub) {
this.connection = con;
this.pubSession = pubSess;
this.subSession = subSess;
this.publisher = pub;

/* Receive message from topic subscriber */
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
} catch (JMSException jmse) {

/* Create and send message using topic publisher */
protected void writeMessage(String text) throws JMSException {
TextMessage message = pubSession.createTextMessage();

/* Close the JMS connection */
public void close() throws JMSException {

/* Run the Chat client */
public static void main(String[] args) {
try {

Chat chat = new Chat();

// Read from command line
BufferedReader commandLine = new

// Loop until the word "exit" is typed
while (true) {
String s = commandLine.readLine();
if (s.equalsIgnoreCase("exit")) {
chat.close(); // close down connection
System.exit(0); // exit program
} else {
} catch (Exception e) {


But at the line

“TopicConnectionFactory conFactory =
(TopicConnectionFactory) jndi.lookup("TopicConnectionFactory");”

I got this 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
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at chat.Chat.<init>(Chat.java:24)
at chat.Chat.main(Chat.java:94)

I realize that the problem is in the jndi setup. But, the problem is that I do not know where to go now and what should I do. I do not know how can I get the jndi.properties file and where and how to put it.

I found a lot of replies on different forums, but no one was very simple to follow. Please, kindly advise from your experience. I would appreciate clear steps to have this program running properly. Otherwise, if you do have a sample program that is already running, please provide it to me.