3

I've got this error message when I compiled a Java file :

error: package javax.servlet does not exist

I installed a big .SH file for Jave EE SDK, a Java version gives me this:

java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

Do I need to install something else?

I am using Tomcat 7 as a Servlet Container located in /Library/Tomcat/ and simple text editor with the command line.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • what IDE are you using ? Is it a runtime or compilation time error ? – Majid Laissi Dec 31 '12 at 18:32
  • Text Editor + Command line ... Majid I've sent you a msg via linkedin ! –  Dec 31 '12 at 18:36
  • 1
    i will check later i don't have access at work – Majid Laissi Dec 31 '12 at 18:47
  • You're using Tomcat, but you still installed Java EE SDK..? Time to clear out some misunderstanding: http://stackoverflow.com/questions/165230/is-the-offical-sun-java-ee-tutorial-the-best-way-to-learn-how-to-make-java-web-a/1876535#1876535 and http://stackoverflow.com/questions/7295096/what-exactly-is-java-ee/7295697#7295697 – BalusC Jan 01 '13 at 01:16
  • I am a little bit lost ... –  Jan 03 '13 at 11:47

4 Answers4

5

You need to include the servlet-api JAR in the compile time classpath.

If you are using maven add this as a dependency in the pom.xml.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

That wil include the dependency at compile time and use the Tomcat one at runtime.

If not you should add Tomcat as project target runtime through Eclipse.

This questions has some useful info on including these in an Eclipse project: How do I import the javax.servlet API in my Eclipse project?

If you are using command line to build the project, you will most likely need to add these to the classpath argument to javac to add these jars to the classpath.

See this question: How to compile servlets from command prompt?

The key part is:

javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
Community
  • 1
  • 1
cowls
  • 24,013
  • 8
  • 48
  • 78
  • I am not using Maven or Eclipse ... plz how to add the Servlet-api to my classpath plz ? –  Dec 31 '12 at 18:32
  • If you aim to do any serious development I strongly suggest downloading an IDE like Eclipse, this will be the first of many issues like this otherwise! – cowls Dec 31 '12 at 18:35
  • Updated answer, again your life will be easier if you use an IDE like Eclipse :) – cowls Dec 31 '12 at 18:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21963/discussion-between-alji-mohamed-and-cowls) –  Dec 31 '12 at 18:43
  • I will give you 25 points of reputation if you help me with this :) –  Dec 31 '12 at 18:43
  • I've fixed your answer with regard to Eclipse. I strongly recommend you to carefully read the "How do I import the javax.servlet API in my Eclipse project?" link yourself. – BalusC Jan 01 '13 at 01:20
2

A Windows User:

I faced this problem myself and here is the solution that worked fine

Just Add this path to your CLASSPATH environmental variable "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar"

The path before the jar name can differ based on your installation. Just go to your lib folder of tomcat and copy the whole directory.

More info for beginners: You can find Environmental variables here MyComputer -> Properties ->Advanced Settings -> Advanced tab

Now you can simply go to cmd prompt and type "javac Myclass.java"

Hope this helps!

praveen
  • 21
  • 2
1
javac -classpath /Library/Tomcat/lib/servlet-api.jar *.java
1

Read about java class paths here on Wikipedia.

Ready closely the last paragraph under "Overview and architecture".

In your example

The javax.servlet package is not part of the bootstrapped or extension packages, so it must be added manually to your classpath. ALJI has shown you how to do this from the command line. The Wikipedia link above also provides examples.

Recommendation

Everyone hits these types issues when starting a new language. Google is full of tutorials that will help you gain a basic understanding of Java class paths.

Matthew Petty
  • 652
  • 5
  • 7