Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Servlet in Package Issue

Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:.
Posts: 43027
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic