I have a VERY simple web server that does accepts client connections, retrieves static web pages and services servlets.
I wrote the web server in Java and I'm using the tomcat library for servlets. I tested all my code by running it in the eclipse IDE and everything works fine, but when I run my code through the command line it starts the main correctly, but as soon as I try to access a web page through the browser (http://localhost/hello.html) then the application crashes, here is a sample output:
p1>java p1Solution.WebServer 80
Waiting for a connection.
Servicing the connection.
Closing the connection and shutting down the executor.
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/Servlet
Exception
at p1Solution.WebServer.main(WebServer.java:54)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 1 more
Here is my WebServer
public class WebServer {
private static final ExecutorService executor = Executors.newCachedThreadPool();
public static void main(String[] args) throws Exception {
int port = (args.length > 0) ? Integer.parseInt(args[0]) : 80;
// listen for clients on the port specified as a command line argument
ServerSocket serverSocket = new ServerSocket(port);
Socket connection = null;
try {
while (true) {
try {
System.out.println("Waiting for a connection.");
// Wait for an in bound client connection
connection = serverSocket.accept();
System.out.println("Servicing the connection.");
// Handle the client connection in a separate thread
executor.execute(new ConnectionHandler(connection));
} catch (Exception e) {
// Never crash the server
e.printStackTrace();
}
}
}finally {
System.out.println("Closing the connection and shutting down the executor.");
connection = null;
serverSocket.close();
executor.shutdown();
executor.awaitTermination(3, TimeUnit.SECONDS);
}
}
}
I've been googling my exception and some people have been mentioning things about the classpath, but I have not found a definitive answer yet. When I run the program in eclipse everything works fine!
Here is the directory of my project (where my project resides):
p1>ls
.classpath bin hello.html p1Solution.jar
.project coreservlets p1Solution testsuite
Here is the directory contents of my p1Solution (where the code resides):
p1>ls p1Solution
ConnectionHandler.class MyHttpServletRequest.class
ConnectionHandler.java MyHttpServletRequest.java
HttpServletRequestAdapter.class MyHttpServletResponse.class
HttpServletRequestAdapter.java MyHttpServletResponse.java
HttpServletResponseAdapter.class WebServer.class
HttpServletResponseAdapter.java WebServer.java
Can anybody help me resolve this issue? Has anybody seen this problem before?
P.S. And yes, I do have the 'ls' command in Windows and it's not magic! Google "ls for windows" if you don't believe me.