• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help! classpath problem

 
Jack Lee
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading and exercising Head First Servlets and JSP, and I follow the instruction of the book to create the servlet in chapter 3. But I failed to compile the BeerSelect.java. Could somebody point out what's wrong with my code or compiling? Thanks.

Tomcat5.5.17 was installed at:
C:\Apache\Tomcat

I am compiling the code right under the Beer directory

The command I inputed was:

javac -cp C:\Apache\Tomcat\common\lib\servlet-api.jar:classes:. -d src\com\example\web\BeerSelect.java

the compile output

src\com\example\web\BeerSelect.java:4: package javax.servlet.http does not exist

import javax.servlet.http.*;
^


My code for BeerSelect.java is as following

package com.example.web;

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

public class BeerSelect extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>");
out.println("<br>Got Beer Color " +
request.getParameter("color"));
}

}
 
Jack Lee
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out.

First I set the classpath:

set classpath C:\Apache\Tomcat\common\lib\servlet-api.jar:classes:.

Then compile
javac -d classes src\com\example\web\BeerSelect.java

However, a little bit confused, what is the difference between this one and previous experience which combined the classpath setup and compiling in one command?
 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at your command line, it appears you are compiling on windows - in which case your classpath separator should be a semi-colon ; rather than a full-colon.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jack,

Daniel is quite correct !

In my first attempt trying to compile the BeerSelect servlet I waste over 15 minutes to figure out what I was doing wrong. (By using the : instead of ; in my Windows XP OS)

Another interesting point is :

If you leave your Tomcat container installed in default directory (C:\Program Files\Apache Software Foundation\Tomcat 5.5) you have to put your -classpath argument within quotes " otherwise your compilation will not work.

Example :

This example is not going to work due to a blank space existing in the full classpath name.

javac -cp C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;classes;. -d classes scr\com\example\web\BeerSelect.java

On the other hand if you use either :

javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar";classes;. -d classes scr\com\example\web\BeerSelect.java

or

javac -cp "%TOMCAT_HOME%"\common\lib\servlet-api.jar;classes;. -d classes scr\com\example\web\BeerSelect.java

where TOMCAT_HOME is a system variable pointing to C:\Program Files\Apache Software Foundation\Tomcat 5.5, everything is going to work fine.

Hope that helps.
[ August 06, 2006: Message edited by: Edisandro Bessa ]
 
Raguel Koning
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heya

I had the same issues. It just stresses *again* how important setting the classpaths are. An interesting site to look at with very helpful tutorials and examples, just to drive the point home would be:

http://www.coreservlets.com/

Especially: http://www.coreservlets.com/Apache-Tomcat-Tutorial/
and the section on setting your classpath:

http://www.coreservlets.com/Apache-Tomcat-Tutorial/#Set-CLASSPATH

Cheers,

R
 
Jack Lee
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Dalton, Edisandro Bessa, and Raguel Koning, thank you very much for your timely help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic