wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets 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:

javac myFirstServlet.java

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
internet detective

Joined: May 26, 2003
Posts: 30076

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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 myFirstServlet.java

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: 60767

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] [Bear's FrontMan] [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


javac myFirstServlet.java

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: 19651

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: 14074

See this entry in the FAQ: Compiling Servlets

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Thank you, now it works fine. I set the environment variable CLASSPATH.
It is sorta covered in the JavaRanch Style Guide.
subject: Compile a Servlet
Similar Threads
Compile a Java EE source code
Servlet programming - Jar files
Servlets And Jsp
Not able to compile first servlet
Servlet does not compile