openJMS chat example

How do i run this chat example on openjms?

package com.ociweb.jms;

import java.io.*;

import javax.jms.*;

import javax.naming.*;

/**

* Simple console-based JMS chat room client.

*/

publicclass ChatRoomimplements MessageListener{

private String name;

private TopicConnection connection;

private TopicSession subscriberSession;

private TopicSession publisherSession;

private TopicPublisher publisher;

private TopicSubscriber subscriber;

/**

* Creates a chat room client.

* @param name the chat room user

* @param filter ignore own messages if true

*/

public ChatRoom(String name,boolean filter)throws Exception{

// set user's name (for chat room display)

this.name = name;

// look up jms connection factory from JNDI

Context context =new InitialContext();

TopicConnectionFactory factory = (TopicConnectionFactory)

context.lookup("JmsTopicConnectionFactory");

// create connection to message server

connection = factory.createTopicConnection();

// look up chat topic from JNDI

Topic topic = (Topic) context.lookup("ChatTopic");

// create chat message publisher

publisherSession =

connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

publisher = publisherSession.createPublisher(topic);

// create chat message subscriber

subscriberSession =

connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

String nameFilter = filter ? name :"";

String selector ="name <> '" + nameFilter +"'";

subscriber =

subscriberSession.createSubscriber(topic, selector,false);

subscriber.setMessageListener(this);

// join chat room

connection.start();

}

/**

* Invoked when someone posts a message to the chat room.

* @see javax.jms.MessageListener#onMessage(javax.jms.Mess age)

*/

publicvoid onMessage(Message message){

try{

TextMessage chatMessage = (TextMessage) message;

String msgSender = chatMessage.getStringProperty("name");

String msgBody = chatMessage.getText();

System.out.println(msgSender +": " + msgBody);

}catch (JMSException e){

e.printStackTrace();

}

}

/**

* Publishes a message to the chat room.

* @param text the message body

* @throws JMSException

*/

publicvoid postMessage(String text)throws JMSException{

// create and publish message

Message message = publisherSession.createTextMessage(text);

message.setStringProperty("name", name);

publisher.publish(message);

}

/**

* Closes the JMS connection.

* @throws JMSException

*/

publicvoid close()throws JMSException{

if (connection !=null){

connection.close();

}

}

publicstaticvoid main(String[] args)throws Exception{

if (args.length == 0){

System.out.println("Usage: ChatRoom <name> [(true|false)]");

System.exit(0);

}

String name = args[0];

boolean filter = args.length > 1

?"true".equalsIgnoreCase(args[1])

:false;

ChatRoom chat =new ChatRoom(name, filter);

boolean done =false;

String text;

BufferedReader in =new BufferedReader(new InputStreamReader(System.in));

while (!done){

text = in.readLine().trim();

if ("bye".equalsIgnoreCase(text)) done =true;

chat.postMessage(text);

}

chat.close();

System.exit(0);

}

}

cos

D:\JAVA_P~1\CHAT3>java -cp C:\openjms\openjms\openjms-0.7.7-beta-1.jar;.; ChatRo

om kasia false

Exception in thread "main" java.lang.NoClassDefFoundError: ChatRoom (wrong name:

com/ociweb/jms/ChatRoom)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:620)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12

4)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

at java.net.URLClassLoader.access$000(URLClassLoader.java:56)

at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

doesnt work

thank you

[7584 byte] By [oll3ia] at [2007-11-27 5:24:22]
# 1
What part of Exception in thread "main" java.lang.NoClassDefFoundError: ChatRoom (wrong name:com/ociweb/jms/ChatRoom)Are you not understanding?
cotton.ma at 2007-7-12 11:50:30 > top of Java-index,Core,Core APIs...
# 2
it doesnt find ChatRoom class but why? it is in that folder
oll3ia at 2007-7-12 11:50:30 > top of Java-index,Core,Core APIs...
# 3
If you have ./com/ociweb/jms/ChatRoom.class you must be in '.' and use the command java com.ociweb.jms.ChatRoom.This is basic Java.
ejpa at 2007-7-12 11:50:30 > top of Java-index,Core,Core APIs...
# 4

i modified the code i run it from bat @start "CHAT to topic" run Chat

have jndi.properties file

and run.bat

@setlocal

@set OJMS_HOME=C:\openjms\openjms

@java -cp %OJMS_HOME%\lib\openjms-0.7.7-beta-1.jar;.; %1 %2 %3

but when i run it the window for chat doesnt open ?what am i doing wrong?

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.naming.*;

import javax.swing.BorderFactory;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.jms.*;

public class Chat implements MessageListener{

int user_number=1;

private String name;

private TopicConnection connection;

private TopicSession subscriberSession;

private TopicSession publisherSession;

private TopicPublisher publisher;

private TopicSubscriber subscriber;

static JTextArea window_messages_sent = new JTextArea(10,10);

JButtonsend= new JButton("Wyslij");

JTextField message_to_send= new JTextField(20);

void printMsg(Message message) throws Exception {

if (message instanceof TextMessage) {

TextMessage text = (TextMessage) message;

/////System.out.println("Received: " + text.getText());

window_messages_sent.setText("User"+user_number+": " + text.getText());

user_number++;

} else if (message != null) {

window_messages_sent.setText("Received non text message");

}

}

public JPanel createPanel(){

JPanel panel = new JPanel();

panel.setBorder(BorderFactory.createTitledBorder("JMS"));

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

//getContentPane().add(panel);

panel.add(window_messages_sent);

panel.add(message_to_send);

panel.add(send);

panel.setVisible(true);

return panel;}

public Chat () throws Exception {

this.name="user"+user_number;

user_number++;

Context context = new InitialContext();

TopicConnectionFactory factory = (TopicConnectionFactory)

context.lookup("JmsTopicConnectionFactory");

connection = factory.createTopicConnection();

Topic topic = (Topic) context.lookup("topic1");

publisherSession =

connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

publisher = publisherSession.createPublisher(topic);

subscriberSession =

connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

subscriber = subscriberSession.createDurableSubscriber(topic,name);

subscriber.setMessageListener(this);

connection.start();

send.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

try{

Message message = publisherSession.createTextMessage(message_to_send.getText());

message.setStringProperty("name", name);

publisher.publish(message);}catch (JMSException e1){ e1.printStackTrace();}

}});

}

public void close() throws JMSException {

if (connection != null) {

connection.close();}

}

public void onMessage(Message message) {

try {

TextMessage chatMessage = (TextMessage) message;

String msgSender = chatMessage.getStringProperty("name");

String msgBody = chatMessage.getText();

////System.out.println(msgSender + ": " + msgBody);

window_messages_sent.setText(msgSender + ": " + msgBody);

} catch (JMSException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws Exception{

Chat chat=new Chat();

JFrame frame = new JFrame("Chat");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(chat.createPanel());

frame.pack();

///f.setLocationRelativeTo(null);

frame.setVisible(true);

///chat.close();

///System.exit(0);

}

}

oll3ia at 2007-7-12 11:50:30 > top of Java-index,Core,Core APIs...
# 5
okey the window opens but whats more important it doesnt send the messages
oll3ia at 2007-7-12 11:50:30 > top of Java-index,Core,Core APIs...
# 6
okey i made it thank you it works now:)
oll3ia at 2007-7-12 11:50:30 > top of Java-index,Core,Core APIs...