Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classpath help with examples

 
Carol Bloch
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to compile the first example from chapter one of the Head First Servlet & Jsp book. It tells me it can't find the package javax.servlet. I seem to always get lost when trying to figure out the classpaths, so I am assuming that is the problem. This is the command I am entering:

javac -classpath c:\program_files\apache_foundation_software\tomcat\common\lib\servlet-api.jar -d classes src\Ch1Servlet.java

Any help?

Carol
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, did you use import javax.servlet.*; correctly ?
Second, make sure the path to the jar file is correct:
dir c:\program_files\apache_foundation_software\tomcat\common\lib\servlet-api.jar
 
Carol Bloch
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have the import in the source file. That is the line the compiler is producing the error. I have the classpath to the jar ok. Maybe it isn't a classpath error at all. If i try to compile it from the directory it resides in, without trying to put it in a jar, it still gives the error.
I have compiled other programs before, but never imported this package. Is it possible the package is missing from my installation? If so, do I just reinstall J2SE?
 
K Bala
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol,

Can u copy and paste your source code here? We can see what went wrong and where.
 
Carol Bloch
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the source code.

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

public class Ch1Servlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
java.util.Date today = new java.util.Date();
out.println("<html> " +
"<body>" +
"<h1 align=center>HF\'s Chapter1 Servlet</h1>" +
"<br>" + today + "</body>" + "</html>");
}
}

My directory structure is
C:\Java\project1\src
with this class in the src folder.

I would appreciate any help. I am pulling my hair out trying to figure this out. I have reinstalled my JDK, installed J2EE, checked the tomcat directory to make sure the servlet-api jar is there and nothing helped.

Carol
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c:\program_files\apache_foundation_software\tomcat\common\lib\servlet-api.jar

is this right?

its usually c:\program files\......
 
Carol Bloch
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My file in the directory is Program Files, but if I try that in the dos command line without the underscore, it gets confused when it hits the space.

Carol
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol you are in luck. I just had this problem... and I was able to fix it.

what you got to do is set your CLASSPATH variable to appropriatly. Then you don't have to type the classpath while compiling your example.

In my case I had to set the CLASSPATH as follows:
OS: Windows XP
start, control panel, system, advanced tab, click on the environment variable.
On the upper list box (user variables, don't touch the system variables),
click New button, then add CLASSPATH on the Variable Name field and I typed E:\Program Files\Apache Software Foundation\Tomcat5.5\common\lib\servlet-api.Jar on the variable value field. You can simply copy the path from explorer and add servlet-api.Jar in the end.

Then compile your code using the following command:

javac -d classes src\Ch1Servlet.java

Let me know if you have any other problem. I encountered some weird problem because of the error on my web.xml file.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean you actually have the jar file in "Program Files", but replaced the spaces in the path with underscores ?

Please try this :

javac -classpath "c:/program files/apache foundation software/tomcat/common/lib/servlet-api.jar" -d classes src\Ch1Servlet.java
 
Carol Bloch
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gyanesh,

Thank you so much!! I had the address in the PATH variable, but when I added it to the CLASSPATH variable, it finally compiled. I really appreciate the help!

Carol
 
J JAR
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this same problem! And yes...I can get the servlet to compile by adding setting the CLASSPATH environment variable in Windows....but I should still be able to compile this by using the -classpath option, right? Does anyone know what I'm doing wrong? Here is my source:

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>");
String c = request.getParameter("color");
out.println("<br>Got beer color " + c);
}
}

Here is the path to servlet-api.jar:
C:\Tomcat\common\lib

Here is what I'm entering with the first couple of errors that get generated:

C:\Data\Java\WebProjects\beerV1>javac -classpath C:\Tomcat\common\lib\servlet-api.jar:classes:. -d classes src\com\example\web\BeerSelect.java
src\com\example\web\BeerSelect.java:3: package javax.servlet does not exist
import javax.servlet.*;
^
src\com\example\web\BeerSelect.java:4: package javax.servlet.http does not exist

import javax.servlet.http.*;
^
src\com\example\web\BeerSelect.java:7: cannot find symbol
symbol: class HttpServlet
public class BeerSelect extends HttpServlet{


I'm sure it's just something stupid that I can't see. Please help!!!

Thanks!
Julie
 
J JAR
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind. The problem was using colons in my classpath instead of semicolons.

Duh.

Thanks anyways.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic