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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compile a Servlet" Watch "Compile a Servlet" New topic

Compile a Servlet

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
I tried to compile a servlet like this:


It does not work, it get the following message:

package javax.servlet does not exist

How can I compile a servlet?

SCJP 1.4
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32819

javax.servlet is part of Java EE not Java SE. Your server (Tomcat, JBoss, etc) comes with a file called j2ee.jar or servlet.jar. Add this to your classpath when compiling.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
I know, that javax.servlet is not part of Java SE. I use Apache Tomcat. I found servlet.jar. So far so good.

How shall I continue?

This is the path:

C:\Programme\Apache Software Foundation\Tomcat 6.0\lib\servlet.jar

javac -classpath C:\Programme\Apache Software Foundation\Tomcat 6.0\lib\servlet.jar

Is it something like that? Or do I have to copy servlet.jar in folder lib of the jdk?

Till now it does not work.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

Do not copy the jar file anywhere. You can include it in the classpath in-place. Otherwise you risk jars getting out of sync with each other.

[Asking smart questions] [About Bear] [Books by Bear]
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Now it worked, I could compile it. I did it like that:

set classpath=C:\Programme\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar



But I do not want to enter the first line always. Is it possible to set it in the operating system, in Microsoft Windows XP you can set in System the variables.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

Use the CLASSPATH variable.

How To Ask Questions How To Answer Questions
Hemavathy Viswanathan

Joined: Dec 26, 2006
Posts: 17
You can set the CLASSPATH as either a user variable or system variable. As the name says, if it is set as a user variable, it is available for the particular user. If it is set as a system variable, the classpath is available to all.

Hemavathy Viswanathan<br />SCJP 1.4/ SCWCD 1.4/ SCBCD 1.3
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15043

See this entry in the FAQ: Compiling Servlets

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Thank you, now it works fine. I set the environment variable CLASSPATH.
I agree. Here's the link:
subject: Compile a Servlet
It's not a secret anymore!