Fog Creek Software
Discussion Board




Running Servlets

Hi:

I am just starting on Servlets and I haven't got too far. I can't seem to
run the program.

System: WinXP, Tomcat 4.1.27

The servlet file is called DontPanic.java - the code is given at the botton
of this email.

The steps I took are:

- I placed the java servlet in C:\Program Files\Apache Group\Tomcat
4.1\webapps\examples\WEB-INF\classes
- I attempted to run this servlet by typing: javac - classpath C:\Program
Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar DontPanic.java
- I received the error message :"javac: invalid flag: Files/Apache""

What have I done wrong - this has been puzzling me for most of today

The code for DontPanic.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class DontPanic {

public void doGet(
  HttpServletRequest request,
  HttpServletResponse response
  )throws IOException, ServletException

  {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();

  out.println("<html>");
  out.println("<head>");
  out.println("<title>Don't Panic</title>");
  out.println("</head>");
  out.println("<body>");
  out.println("<h1>Don't Panic</h1>");
  out.println("</body>");
  out.println("</html>");
  }

Technophobe :(
Sunday, October 19, 2003

I guess the problem is the space in the path. Try this
javac -classpath "C:\Program Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar" DontPanic.java

David

David Rabinowitz
Sunday, October 19, 2003

javac compiles it for you. running is a different matter.

There's spaces in the path to the jar file which the command line doesn't understand. Try putting double quotes around "C: ...jar" and try again. I don't have a dos machine so I can't test the space and quotes stuff -- I'm telling you what you'd do in Unix.

Ed the Millwright
Sunday, October 19, 2003

Hi,

This happens because you have spaces in the argument you are passing to the classpath switch. Put quotes around the part that starts with C:\Program, and end the quotes after the word servlet.jar.

An easier way to compile your servlet is to copy (don't move) the file servlet.jar into the SDK extensions folder. If your SDK is installed in C:\j2sdk, then copy the file to the folder named C:\j2sdk\jre\lib\ext. That way you don't have to use the classpath switch every time you compile. Also, if you do use the classpath switch, make sure you don't have a space after the -. It should be -classpath, not - classpath.

--Steve

Steve Gilbert
Sunday, October 19, 2003

Your class doesn't extends HttpServlet class. Could this be the problem. I added the extends clause and ran it in the browser and it works fine.

Pushkal Mishra
Sunday, October 19, 2003

Take a look at resin  http://www.caucho.com for your servlet engine instead of tomcat.    I find that resin is much easier to configure and has a quicker startup than tomcat.

Running servlets under eclipse using resin and the resin plugin http://membres.lycos.fr/resinforeclipse/ makes for a pretty sweet development environment for servlets. You can debug them as they run and when using JDK 1.4 you can edit them and replace them on the fly.  You can do this with tomcat as well. 

Servlets Rock
Sunday, October 19, 2003

First of all, what do you mean by "I placed the java servlet in [...]"? What did you put in there? I ask because you're having trouble compiling the DontPanic class, and the compiled class is what should go in that directory.

Putting servlet.jar (or almost anything else, really) in the ext dir just to make the compiler command line shorter is a *terrible* idea. It will come back and bite you when an updated servlet.jar is released and your servlet container won't start.

If you want to make compilation easier, use a tool like ant.
http://ant.apache.org/

Daryl Oidy
Monday, October 20, 2003

Simple it doesn't like the space in program files, also you will need to make sure all the .jars that contain the classes you used are in the class path

the artist formerly known as prince
Monday, October 20, 2003

Running a Servlet on command prompt , little bit weird!

I suggest to read some good book or tutorial before running any servlet!!!

BSA
Monday, October 20, 2003

He's not trying to run it just compile it

the artist formerly known as prince
Monday, October 20, 2003

the artist formerly known as prince -
1. He should have given thread name as "compiling Servlets"
2. Looking at mistakes done in his program, it seems he is very new to Servlets and needs to read some good book or atleast servlet tutorial before posting his problem to any forum.

BSA
Tuesday, October 21, 2003

Having struggled with the RTFM (and come through) phase of learning Servlets, I don't get at all upset at people who ask on a forum.

The FMs almost universally are written as if you already know everything.  The FMs for Java in general expect you to know a million design patterns before you can get anything done.

Leaving a space in a classpath is a perfectly normal mistake and does not imply that you haven't tried to RTFMs.

Richard Ponton
Tuesday, October 21, 2003

*  Recent Topics

*  Fog Creek Home