0

I am trying to compile a simple class that imports some servlet libraries. Since I have not added the tomcat directory to the classpath, what is the good practice way to let the system know where to look for the servlet libraries which are found in the tomcat/lib folder?

Currently, the system just gives errors like this:

ackage javax.servlet does not exist
[javac] import javax.servlet.ServletException;
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Genadinik
  • 18,153
  • 63
  • 185
  • 284
  • What is your environment? Eclipse? NetBeans? Maven? Ant? CLI with plain javac? – musiKk Apr 08 '11 at 20:49
  • @musiKk I use Eclipse in Ubuntu. But shouldn't this be independent of the IDE? – Genadinik Apr 08 '11 at 20:52
  • 1
    It is related to the tool you use to compile. If you compile with your IDE, then you must set it in your IDE so it works. Anyway that is just adding the jars (servlet.jar) to the classpath, the way your IDE wants it added to. – SJuan76 Apr 08 '11 at 20:57
  • Right, its a path issue, but you generally should not add the servlet jars to the path, so I was just wondering how this is typically done. – Genadinik Apr 08 '11 at 21:04
  • 1
    You need to integrate the servletcontainer (Tomcat) in IDE and associate it with the web project. See also http://stackoverflow.com/questions/4076601/how-do-i-import-the-javax-servlet-api-in-my-eclipse-project/4076706#4076706 – BalusC Apr 08 '11 at 21:14

5 Answers5

2

If you want to use only the java-provided tools, you will have to look at the JavaDocs for the javac tool and add the desired jars to the classpath by hand.

Unless this is a learning exercise and you are trying to do it the "hard way", I would recommend looking into Maven, Ant, or your favorite IDE, as they will be more useful in your future career.

cjstehno
  • 13,468
  • 4
  • 44
  • 56
  • Yeah I was at first trying to understand how to do it the right way before I settle into an easy way of doing it via an IDE. – Genadinik Apr 08 '11 at 23:26
1

I had the same problem in eclipse.

To fix it,

Right click on your web project in project explorer -> properties -> Project Facets -> press ALT+R -> select server you want -> Apply ->Ok and done.

Note: I am using Eclipse Helios.

You should have already added server in eclipse. Installing Apache Tomcat Server in eclipse

Laxman
  • 2,643
  • 2
  • 25
  • 32
1

If you using ecplise, you can add application server runtime library to your build path or You can add jar from other folder to your java build path configuration.

Adi Sembiring
  • 5,798
  • 12
  • 58
  • 70
1

So you're actually asking how to compile a servlet class using javac? You just need to put the dependencies in the compiletime classpath the usual Java way. This is not specifically related to Servlets or something.

You can do that using the -cp or -classpath argument of javac. It accepts a colonseparated string of (relative) paths to package root folders and JAR files.

javac -cp .:/path/to/Tomcat/lib/servlet-api.jar com/example/MyServlet.java

The . in the classpath stands for current directory. In Windows, the path separator is by the way a semicolon.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

It depends of your IDE. Each good Java IDE provides a way to specify an extra-classpath.

If you use Maven as build tool, the good way is to specify a "provided" scope for theses libraries. They will be used at compile time but not packaged in the final war:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>
Benoit Courtine
  • 7,014
  • 31
  • 42