This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet in Package Issue" Watch "Servlet in Package Issue" New topic
Author

Servlet in Package Issue

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Hi,
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 JAVA_HOME=C:\JDK1.5
SET CATALINA_HOME=c:\Tomcat
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\classes
D:\Head\MyProjects\beerv1\etc
D:\Head\MyProjects\beerv1\lib
D:\Head\MyProjects\beerv1\src
D:\Head\MyProjects\beerv1\web\form.html (Html file is kept here)

D:\Head\MyProjects\beerv1\src\com\example\model
D:\Head\MyProjects\beerv1\src\com\example\web\BeerSelect.java (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
C:\Tomcat\webapps\Beer-v1\WEB-INF\classes
C:\Tomcat\webapps\Beer-v1\WEB-INF\classes\com\example\web
C:\Tomcat\webapps\Beer-v1\WEB-INF\classes\com\example\model
C:\Tomcat\webapps\Beer-v1\WEB-INF\lib
C:\Tomcat\webapps\Beer-v1\WEB-INF\web.xml (xml file is kept here)


PROBLEM !!!
---------------
If I am compiling the below one,
D:\Head\MyProjects\beerv1\src\com\example\web>javac BeerSelect.java

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 BeerSelect.java 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 BeerSelect.java 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,
Niyas.
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 "My.java" that's in package "com.example.web" by making the current directory "com/example/web" and typing "javac My.java", 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.

--Dale--
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/BeerSelect.java

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
Marshal

Joined: Mar 22, 2005
Posts: 41073
    
  43
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).


Ping & DNS - my free Android networking tools app
 
jQuery in Action, 2nd edition
 
subject: Servlet in Package Issue
 
Similar Threads
Package doesn't exists - error!!- How to resolve?
Headfirst ch3 Beerselect submit button not working
Need help in compiling programs
Compiling custom imports
Getting package and class exceptions with the BeerAcvice web app!!! Pls Help