Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes servlet compilation error. 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 compilation error." Watch "servlet compilation error." New topic
Author

servlet compilation error.

yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
I have the same old problem of Servlet Classpath settings.Someone please help.

I use Apache Tomcat4.1,j2sdk1.4.2_08 on windows XP.
I am able to do the foll ,
1.See the Apache Tomcat/4.1.31 homepage and execute the examples of that.
2.Compile all other java files.
3.execute simple JSP examples like date display etc.
but unable to compile servlets.

I set the path and classpath in Environment Variables(XP).
path=C:\Sun\AppServer\bin;C:\j2sdk1.4.2_08\bin;C:\program files\apache group\tomcat4.1\Common\lib\Servlet.jar
CLASSPATH=.;C:\Sun\AppServer\bin;C:\j2sdk1.4.2_08\bin;C:\program files\apache group\tomcat4.1\Common\lib\Servlet.jar

but still I get
javax.servlet does not exist.

I am sure that am missing some simple step,please help me out
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

First, you don't need to put jar files in your system path or bin directories in your classpath. Doing so just makes it harder to debug these types of issues.

Try just putting servlet.jar (watch case) on your classpath.
Also, I know that, with at least some versions of Windows, you need to wrap your classpath/path env variables with quotes if there are spaces in the directory names.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
Thank you for your help.
I have done the mentioned changes, but still the problem persists even after restart,
Now my path and classpath are,

CLASSPATH="C:\Program Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar"

path=C:\Sun\AppServer\bin;C:\j2sdk1.4.2_08\bin;

The path on which servlet.jar resides ac copied from its properties is,
C:\Program Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar

Please help
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi
Visit this link Tomcat Configuration

bye for now
sat
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
As a first step, try specifying the CLASSPATH explicitly in the javac command:

javac -classpath "C:\Program Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar" <yourServlet>.java


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12758
    
    5
Personally, I do all servlet compiling through ANT because I can use the build.xml file to define the exact classpath needed for any particular project on the fly. My system environment variables stay clean and I can switch projects without any chance of confusion between library versions. Yes there is a learning curve but well worth the effort.
(Plus, of course, major open source Java toolkits ship with build.xml files included.)
Bill
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
As a first step, try specifying the CLASSPATH explicitly in the javac command:

javac -classpath "C:\Program Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar" <yourServlet>.java


I have tried and it doesnt help.But the example servlets in Tomcat seem to work fine.I am damn sure am missing something very silly but could not figure out.PLEASE HELP
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I have tried and it doesnt help. But the example servlets in Tomcat seem to work fine. I am damn sure am missing something very silly but could not figure out.


Wait.
What doesn't work?
Are you trying to compile or run a servlet?
The classpath has nothing to do with running servlets.
Tomcat ignores your classpath.

You DO need servlet-api.jar on your class path to compile your servlet.

What are you trying to do that isn't working?
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
Am trying to compile the servlet and not run the servlet. In the lib file I just find a servlet.jar file and not servlet-api.jar.I use Tomcat4.1.
Do I have to download a Servlet-api.jar file from somewhere apart from the
normal installation of tomcat..??

Sorry for getting confused between running and compiling and also confusing you.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, servlet.jar is fine (my mistake there).
What errors are you getting when you try to compile your servlet with that classpath?
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
the error I get is ,package javax.servlet does not exist.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi

Servlet.jar is not in the Classpath. Check again.

or check my previous answer and read the article how to install & configure tomcat

bye for now
sat
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
My current classpath as copies from the CLASSPATH variable is as follows,

[<B>] CLASSPATH= .;"C:\Program Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar" [</B>]

if you refer to the reply to Ben Souther .My classpath does have servlet.jar file.I havent change my settings yet.Only change I have made to it after that is to add a . to include currentdirectory.

The link you gave does give info abt Configuring Tomcat and deploying
webapplication.Am not able to relate it to servlet compilation.My tomcat
is working fine and even samples are working fine.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What you've got should work.

Here's one way to triple check your classpath.

1.) From the command line, type:
echo %CLASSPATH% > MYCP.txt

2.) Open MYCP.txt with notepad and copy the part of the classpath
that points to the common/lib directory.

3.) Open Explorer and paste the path into the address field.

Explorer should be pointing to the directory with the jars.
You should see servlet.jar in there.
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
Ben, It does point to the dirctory where servlet.jar file is present.
I have a doubt with the sample am code am testing with,
please hav a look,
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class helloworld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}

I am using javac helloworld.java to compile
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

And what error are you seeing when you try to compile it?


PS: It's always a good idea to stick to Java naming conventions.
Classnames should start with an upper case letter (Helloworld.java).
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
the error
helloworld.java:2 : p ackage javax.servlet does not exist
import javax.servlet.*;
helloworld.java:3 : p ackage javax.servlet.http.*; does not exist
import javax.servlet.http.*;

etc...

Even the javap javax.servlet.http.HTTPServlet; does not work
ERROR:could not find javax.servlet.http.HTTPServlet;

I have tried unistalling and reinstalling TOMCAT but still doesnt work.

please suggest any other server to learn servlets..
[ July 24, 2005: Message edited by: yash Vi ]
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
: and p together has given a smiley .. please replace the smiley with
: p while reading
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by yash Vi:
: and p together has given a smiley .. please replace the smiley with
: p while reading



You can edit your post - use the icon that looks like a paper and pencil. Put a space in to prevent the smiley. Or select the box right at the bottom of the input form (Scroll down and check "Disable smilies in this post").


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi yashvi


I am sure your jar file is not in the classpath. So that you are getting the same error. Tomcat is simple to configure and learn servlets.
your code is correct. verify the configuration of Tomcat again

bye for now
sat
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
I have checked it and the classpath in my previous reply was copied from the environment variable-> classpath-> edit and hence i feel that there is not a possibility of typo too.I followed wot ben adviced and cud view the
directory of the jar files..Is there anything else to be done apart from classpath....
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi
Your class path is
[<B>] CLASSPATH= .;"C:\Program Files\Apache Group\Tomcat 4.1\common\lib\servlet.jar" [</B>]



But no
servlet-api.jar in your classpath. Change from servlet.jar to servlet-api.jar in your classpath.
your classpath should be like this


I hope, now it will work.
bye for now
sat
yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
hi,I amusing tomcat4.1 and there is no sevlet-api.jar in the lib only servlet.jar is present.As discussed with ben earlier ,shd I dowload this servlet-api.jar.?
chsanthosh kumar
Ranch Hand

Joined: Jun 28, 2005
Posts: 56
Please servlet-api.jar

Download it from here.


---------------------<br />SCJP(1.4)With 95%<br /> <br />Do it Right and Forget it!
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
You don't need to download it. Use the one in [TOMCAT_HOME]/common/lib

In Tomcat4 it was servlet.jar
In Tomcat5 the split it into servlet-api.jar and jsp-api.jar

Try moving it into a directory that doesn't contain spaces in it.
ie copy it to C:\java\servlet.jar, add that to your classpath and try compiling again
samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
hey i m also getting same problem as same that of yash
javax.servlet does not exist

i already set path and classpath

yash now do u able to compile servlet
samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
sorry guys my servlet compiles after setting the classpath again
than restarting the machine it works
keris joyo
Greenhorn

Joined: Jul 28, 2005
Posts: 1
you must download a jar file of servlet.jar into the directory where ${tomcat_home}/common/lib.if that,you will compile servlet!


I'm a chinese student.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet compilation error.
 
Similar Threads
J2ee setup
urgent help in running examples from hf jsp servlets
servlet help!
Compilation problem in servlets
Basic Doubts