Hello everyone i'm working on getting this ChatApplet to work. It's an example from Oreilly Java Servlet Programming on creating a chat system using applet servlet communication. The problem i'm having is the applet returns this exception.
Java(TM) Plug-in: Version 1.4.1_06
Using JRE version 1.4.1_06 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Brian Swingle
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.lang.NoClassDefFoundError: com/oreilly/servlet/HttpMessage
at HttpChatApplet.getNextMessage(HttpChatApplet.java:70)
at HttpChatApplet.run(HttpChatApplet.java:98)
at java.lang.Thread.run(Unknown Source)
java.lang.NoClassDefFoundError: com/oreilly/servlet/HttpMessage
at HttpChatApplet.broadcastMessage(HttpChatApplet.java:111)
at HttpChatApplet.handleEvent(HttpChatApplet.java:135)
at java.awt.Component.postEvent(Unknown Source)
at java.awt.Component.postEvent(Unknown Source)
at java.awt.Component.postEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
But the Applet was compiled with this package on my PC. The applet its self is being run from a directory just inside my domain. And this package is included located in tomcat /lib folder but with the applet not being run from tomcat it cant fint the .jar file. How do i fix this exception?
here is the applet code.
----------------------------Applet---------------------------------
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import com.oreilly.servlet.HttpMessage;
public class SocketChatApplet extends Applet implements Runnable {
static final int PORT = 2428;
DataInputStream serverStream;
TextArea text;
Label label;
TextField input;
Thread thread;
String user;
public void init() {
// Check if this applet was loaded directly from the filesystem.
// If so, explain to the user that this applet needs to be loaded
// from a server in order to communicate with that server's servlets.
URL codebase = getCodeBase();
if (!"http".equals(codebase.getProtocol())) {
System.out.println();
System.out.println("*** Whoops! ***");
System.out.println("This applet must be loaded from a web server.");
System.out.println("Please try again, this time fetching the HTML");
System.out.println("file containing this servlet as");
System.out.println("\"http://server
ort/file.html\".");
System.out.println();
System.exit(1); // Works only from appletviewer
// Browsers throw an exception and muddle on
}
// Get this user's name from an applet parameter set by the servlet
// We could just ask the user, but this demonstrates a
// form of servlet->applet communication.
user = getParameter("user");
if (user == null) user = "anonymous";
// Set up the user interface...
// On top, a large TextArea showing what everyone's saying.
// Underneath, a labeled TextField to accept this user's input.
text = new TextArea();
text.setEditable(false);
label = new Label("Say something: ");
input = new TextField();
input.setEditable(true);
setLayout(new BorderLayout());
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
add("Center", text);
add("South", panel);
panel.add("West", label);
panel.add("Center", input);
}
public void start() {
thread = new Thread(this);
thread.start();
}
String getNextMessage() {
String nextMessage = null;
while (nextMessage == null) {
try {
// Connect to the server if we haven't before
if (serverStream == null) {
Socket s = new Socket(getCodeBase().getHost(), PORT);
serverStream = new DataInputStream(
new BufferedInputStream(
s.getInputStream()));
}
// Read a line
nextMessage = serverStream.readLine();
}
catch (SocketException e) {
// Can't connect to host, report it and wait before trying again
System.out.println("Can't connect to host: " + e.getMessage());
serverStream = null;
try { Thread.sleep(5000); } catch (InterruptedException ignored) { }
}
catch (Exception e) {
// Some other problem, report it and wait before trying again
System.out.println("General exception: " +
e.getClass().getName() + ": " + e.getMessage());
try { Thread.sleep(1000); } catch (InterruptedException ignored) { }
}
}
return nextMessage + "\n";
}
public void run() {
while (true) {
text.appendText(getNextMessage());
}
}
public void stop() {
thread.stop();
thread = null;
}
void broadcastMessage(String message) {
message = user + ": " + message; // Pre-pend the speaker's name
try {
URL url = new URL(getCodeBase(), "/servlet/ChatServlet");
HttpMessage msg = new HttpMessage(url);
Properties props = new Properties();
props.put("message", message);
msg.sendPostMessage(props);
}
catch (SocketException e) {
// Can't connect to host, report it and abandon the broadcast
System.out.println("Can't connect to host: " + e.getMessage());
}
catch (FileNotFoundException e) {
// Servlet doesn't exist, report it and abandon the broadcast
System.out.println("Resource not found: " + e.getMessage());
}
catch (Exception e) {
// Some other problem, report it and abandon the broadcast
System.out.println("General exception: " +
e.getClass().getName() + ": " + e.getMessage());
}
}
public boolean handleEvent(Event event) {
switch (event.id) {
case Event.ACTION_EVENT:
if (event.target == input) {
broadcastMessage(input.getText());
input.setText("");
return true;
}
}
return false;
}
}
---------------------------End of Applet-----------------------
Thanks,
Brian