aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes classpath help with examples Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "classpath help with examples" Watch "classpath help with examples" New topic
Author

classpath help with examples

Carol Bloch
Greenhorn

Joined: Nov 21, 2005
Posts: 18
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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


[My Blog]
All roads lead to JavaRanch
Carol Bloch
Greenhorn

Joined: Nov 21, 2005
Posts: 18
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

Joined: Jul 14, 2005
Posts: 28
Carol,

Can u copy and paste your source code here? We can see what went wrong and where.


Quitters never win; Winners NEVER QUIT!!!
Carol Bloch
Greenhorn

Joined: Nov 21, 2005
Posts: 18
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

Joined: Aug 25, 2005
Posts: 536
c:\program_files\apache_foundation_software\tomcat\common\lib\servlet-api.jar

is this right?

its usually c:\program files\......


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Carol Bloch
Greenhorn

Joined: Nov 21, 2005
Posts: 18
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

Joined: Nov 27, 2005
Posts: 47
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 21, 2005
Posts: 18
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

Joined: Jan 10, 2006
Posts: 4
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

Joined: Jan 10, 2006
Posts: 4
Nevermind. The problem was using colons in my classpath instead of semicolons.

Duh.

Thanks anyways.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath help with examples