File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet in Package Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet in Package Issue" Watch "Servlet in Package Issue" New topic

Servlet in Package Issue

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
in the
I am trying to run my first servlet prgoram, but in vain. I will give the full details what I am trying to do.

I am using Head First Servlets & JSP Book. And I followed the example given in that book.

All the below things are tried in Windows 98 SE (PC, not in network)
I am using JDK1.5, which is in C:\JDK1.5
I am using Tomcat 5.0.38, which is in C:\Tomcat

Environment Variables
I am setting my environment variables in the autoexec.bat:

SET PATH=C:\JDK1.5\bin\;%PATH%
SET CLASSPATH=.;c:\Tomcat\common\lib\servlet-api.jar;c:\Tomcat\common\lib\jsp-api.jar;%CLASSPATH%

My development Folder details
D:\Head\MyProjects\beerv1\web\form.html (Html file is kept here)

D:\Head\MyProjects\beerv1\src\com\example\web\ (Servlet file is kept here)

My Deployment folder details
C:\Tomcat\webapps\Beer-v1\WEB-INF\form.hmtl (HTMl file is kept here)
C:\Tomcat\webapps\Beer-v1\WEB-INF\web.xml (xml file is kept here)

If I am compiling the below one,

It is compiling and the class file is created.

If I want to compile this file in the package, it showing error like
"import javax.servlet.*" package doesn't exists

I know something wrong with the classpath.
What are all the things needs to be set in the classpath.
I have set for servlet-api.jar and jsp-api.jar. Other than that we have to set any other thing.
I have given all the folder structure above. please suggest how to set the classpath correctly so that while compiling the its class(BeerSelect.class) should be automatically placed in "C:\Tomcat\webapps\Beer-v1\WEB-INF\classes\com\example\web"

I have web.xml file, form.html file and Servlet file. There is no problem with these files. I think it is because of classpath issue. I have referred lots resources for classpath, but I couldn't make it success.

Thanks in advance,
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
There's a special forum concerning J2EE certification where Kathy and Bert hang out. You might consider asking your question in that forum.

But a quick look at your situation makes me think that you should specify '-classpath ???/com/example/web' on your compile statement. One thing I have found is that you can't compile "" that's in package "com.example.web" by making the current directory "com/example/web" and typing "javac", even if you have "." in the classpath environment variable. Your best bet is to not have a classpath environment variable at all, and write a compile batch file with a -classpath parameter. Since it's in a batch file, once you figure it out, you've got it.

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
you are telling -classpath is missed in the compiler option.

javac -classpath c:/tomcat/common/lib/servlet-api.jar:classes:. - d classes src/com/example/web/

In the above thing, whether -classpath alone is enough or in the place of -classpath we have to mention anything. That is whether -classpath is keyword used in the compiler option.

I am having servlet-api.jar file c:/tomcat/common/lib/ folder. But why we are using like this: servlet-api.jar:classes:.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
Besides having to include the servlet API in your classpath, you should compile from the "src" directory, not some subdirectory of it. Otherwise javac will be confused about your package structure. As to putting the class files into the proper directory, check out the "-d" option of javac; it specifies where to put class files (in your case it would be "-d ..\classes" since you're in the "src" directory).
I agree. Here's the link:
subject: Servlet in Package Issue
It's not a secret anymore!