1

I have a Java solution that needs to include servlet-api.jar in my classpath.

It works if I copy the servlet-api.jar from my tomcat folder to the project folder. But when I upgrade to Java7, the servlet-api.jar is a new version and will need to be replaced.

Is there anyway I can get around this issue? I don't think I can point it directly to my tomcat folder on my local development machine...

informatik01
  • 16,038
  • 10
  • 74
  • 104
daniely
  • 7,313
  • 5
  • 29
  • 46
  • You shouldn't at all. Duplicate of [How do I import the javax.servlet API in my Eclipse project?](http://stackoverflow.com/questions/4076601/how-do-i-import-the-javax-servlet-api-in-my-eclipse-project) – BalusC Jun 03 '13 at 19:25
  • Your question is not clear, do you get any errors/messages? – A4L Jun 03 '13 at 19:28
  • 1
    @BalusC: this works fine if only Eclipse is used to compile the project, but if a real build tool is used, the appropriate servlet api jar must be in the classpath somehow, and if a dependency management tool like Gradle, Ivy or Maven is not used, the jar should be in the project. Just not in the deployed war. – JB Nizet Jun 03 '13 at 19:30
  • @JBNizet: the way he put the question suggests that he has it in webapp's own runtime classpath, which is definitely Wrong™. Otherwise, I don't see any sensible underlying reason why such a strange question is been asked. Dependency management tools like Maven do not work like that. They have their own way of managing dependencies independent from JARs which are manually placed in the project. – BalusC Jun 03 '13 at 20:47
  • 1
    I agree. My point was that although your linked answer explains very well how to use Eclipse to reference the servlet-api jar through a server configuration, it doesn't explain how to do when eclipse is out of the picture (and the project uses ant, for example), and it also doesn't explain how to solve the OP problem: being able to target both Tomcat 6 and Tomcat 7. – JB Nizet Jun 03 '13 at 21:15

2 Answers2

2

The problem is not the choice of Java 7 vs. another version of the JDK. The problem is: which version of the servlet API specification do you want to use in your webapp. This choice of course also has an impact on the servlet container you'll be able to use. For example, if you choose servlet 3.0, you will have to use Tomcat 7, and not Tomcat 6.

So, first choose which version of the API (and thus container) you want to use, then add the corresponding jar to your project, and build with this jar in your classpath. Just make sure NOT to add this jar file in the WEB-INF/lib folder of your deployed webapp. The jar should only be used for compiling. At runtime, the container already has this jar in its own classpath.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I would like the website to work on servers with either Tomcat 6 or 7 installed. Is it possible not to include the jar at all in my web application? – daniely Jun 03 '13 at 20:24
  • Your deployed webapp **MUST NOT** include the jar file. You must only use it to compile your app (I thought that was clear, since it was already capitalized in my answer. So I repeat it here in bold). Since you want to be able to deploy the app on Tomcat 6, use the servlet-api.jar coming with Tomcat 6 to compile your app. The API and Tomcat are backward-compatible, so your app will run fine, unmodified, on Tomcat 7. – JB Nizet Jun 03 '13 at 20:26
  • When the `WAR` file is built, where are the compile-time jars placed? (I understand run-time jars are placed in `WEB-INF/lib`. I'm wondering where compile-time jars are located in the built `WAR`) – theyuv Apr 10 '22 at 18:03
0

I do not like having servlet api jar to satisfy ant build. Say, we move from Tomcat 6.0.42 to 6.0.43 or 7.x.x. Or to Glassfish. Someone has to make sure to get the latest jar from the tomcat installation and replace it in the project.

Renaming servlet-api.jar to servlet-api-tomcat-6.0.42.jar may be one solution. It also might indicate to the developer that it must be replaced when we change the Web Container. Is there a cleaner solution?

RuntimeException
  • 1,593
  • 2
  • 22
  • 31