Genadinik
Genadinik

Reputation: 18629

Java compilation gives errors about servlet libraries

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;

Upvotes: 0

Views: 711

Answers (5)

Laxman
Laxman

Reputation: 2690

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

Upvotes: 1

BalusC
BalusC

Reputation: 1108567

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.

Upvotes: 1

Adi Sembiring
Adi Sembiring

Reputation: 5936

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.

Upvotes: 1

cjstehno
cjstehno

Reputation: 13984

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.

Upvotes: 2

Benoit Courtine
Benoit Courtine

Reputation: 7064

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>

Upvotes: 0

Related Questions