Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How to compile servlets with servlet-api.jar 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How to compile servlets with servlet-api.jar" Watch "How to compile servlets with servlet-api.jar" New topic
Author

How to compile servlets with servlet-api.jar

avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
excuse me,I was a freshman to get in touch with JSP and Servlet.
I only installed j2sdk1.4.2_03 and Tomcat 5.030
I typed next instruction in command-line.

But it does not work.
I only installed j2se(j2ee is too big for me),so how can I compile servlet with Tomcat's servlet-api.jar?


My Way,My Pace
Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
In windows environment, classpath seperator should be semi-colon ';' instead of colon ':'

My system classpath is:

.;C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\jsp-api.jar

and all servlets code compiles just fine.


SCWCD 1.4<br />---------------------<br />Ability is what you're capable of. <br />Motivation determines what you do. <br />Attitude determines how well you do it.<br />---------------------
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Hello boss,
You can try alternatively
By first giving .:classes:C:\tomcat\common\lib\servlet-api.jar

One more solution
Give in the classpath ,:c:\dev\classess;C:\tomcat\common\lib\servlet-api.jar

c:\dev\classes varies if u have different folder for classess.


SCJP 1.4 SCWCD 1.4 SCDJWS 1.4
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
The code you have given must be working fine in Linux environment.
i think you are follwoing HEAD FIRST SERVLETS AND JSP.

In Windows the classpath separation is ; and in linux or Unix it is :
Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
Hi Antohy,

Please help us in resolving your problem. What environment you are working on? Linux or Windows??

If you are using Linux, then why your classpath has windows-fashioned driveletter C:\ and back slash etc?

If you are running on windows, then why your classpath has colon seperator instead of semicolon?

I would suggest to try compiling simple java program first and then try compiling a servlet after adding servlet-api.jar correctly on your classpath.

Try setting classpath first and then display it using echo command and then try simple javac, let us know how things work at your end.

Enjoy intial setup
[ June 19, 2005: Message edited by: Anand Wadhwani ]
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
sorry!
I will explain more detail next time.
I just type it followed the book.
Because your helps,I can compile it successfuly.
thx a lot!
Sanjivi Syamsundar
Greenhorn

Joined: Jun 17, 2005
Posts: 6
I would like to add that the -classpath option overrides the existing classpath and uses only the jar mentioned in the command line. I had problems while compiling a servlet with 2 jar files. I added this one to the classpath in windows to overcome the problem.
Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
In case antohy doesn't know... another way of doing it without adding to window's classpath could be:

java -classpath %CLASSPATH%;... ... ...

Have Fun!

:-)
[ June 20, 2005: Message edited by: Anand Wadhwani ]
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
Sorry~I was confused.
I installed J2SE 1.4.2_08,tomcat 5.030,and worked on Windows xp.
I tried to type some instructors in command line.
step 1:

it does not work and looks the same as I type javac-help.

Step 2:

compile fail!Because javax.servlet package does not exist.

Step 3

it does not work and looks the same as I type javac-help.

So what is it going on and how can I solve this problem?

If I wnat my .class file wiil end up in /Test/classes/com/example/web/,what instruction should I type?

thx a lot!
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
Hi Antohy,

If you want to specify the destination folder, you can do it by using -d options of javac. Btw, in windows you separate classpath with a semi colon ( not with a colon. colon is used in linux.

so, if you want to specify the classpath for compiling and destination of the class file, you should do the following

I am considering the following structure.

tomcat home = d:\tomcat
web app = d:\tomcat\webapps\Test

d:\>javac -classpath d:/tomcat/common/lib/servlet-api.jar;d:/tomcat/webapps/Test/WEB-INF/classes
-d d:/tomcat/webapps/Test/WEB-INF/classes d:/tomcat/webapps/Test/TestServlet.java

Now you need to do the following :

1) you need to set your class path to the location where servlet-api.jar resides. here thst is, d:/tomcat/common/lib/servlet-api.jar

if you want to add some other location also, seoarate that with a semi colon, as I have done to add d:/tomcat/webapps/Test/WEB-INF/classes. I am considering that you are using other classes in your TestServlet.java, some Beans or whatever.

2) specify you destination location after -d option. I am considering that you want your class file to be in d:/tomcat/webapps/Test/WEB-INF/classes. So your TestServlet.class will be generated in this folder.

3)At last, specify the location of your source file. here that is d:/tomcat/webapps/Test/TestServlet.java.

4) If you have package statement in your servlet, say
package com.example.web;

then you need to specify the parent directory of "com" as the destination folder, but you have to specify the full path to source file.

Do Remeber that you can use relative paths also.

hope this helps,
Regards,

Sushma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to compile servlets with servlet-api.jar
 
Similar Threads
Head First code compiling?
without using any editor how to compile a servlet?
Cannot find symbol
compiling servlet and tomcat
oobtaining servlets.class file independently