14

I have a project that requires some Tomcat libs to run properly. I installed Tomcat (on Mac) which is essentially just a put-the-folder-somewhere process. I originally resolved the issue by adding the needed jar files as external jars for the project - however this messes with the project .classpath which I can't allow - those changes would be tracked in our source control.

I've tried adding the tomcat/lib directory to eclipse as a classpath variable but that doesn't resolve the issue.

When the tomcat server starts, I receive ClassNotFoundException: HttpServletRequest.

How else can I add the tomcat lib directory so that I'm not modifying the project classpath?

helion3
  • 34,737
  • 15
  • 57
  • 100
  • 1
    You can not add any JARs to a project's build path without it altering the `.classpath` file. Runtime, however is a different story. Anyway, this is exactly the kind of problem that Class Variables were intended to solve; what, exactly, do you mean by "that doesn't resolve the issue?" – E-Riz Oct 24 '13 at 20:09
  • Adding a new "classpath variable" to the eclipse menu has no effect. The code continues to show red errors under the missing tomcat code references, and starting the tomcat server (from within eclipse) errors out with the above errors. The same results. – helion3 Oct 24 '13 at 23:30
  • More details would help, such as step-by-step descriptions of what you did, or even some screen shots like some of the answers below. I've used Classpath Variables many times to solve this kind of problem, so I suspect you're just missing something about using them. Have you tried reading the help about them? – E-Riz Oct 25 '13 at 02:04
  • I probably did miss something. I went in to eclipse -> preferences -> java -> build path -> classpath variables. I added a new variable called tomcat_libs, pointed it at the tomcat lib directory. I'm guessing that's not enough to tell eclipse to include those JARs, because nothing changed. I can only fix the issue by specifically including the JARs in the project. – helion3 Oct 25 '13 at 16:55
  • 1
    Creating a Classpath Variable is only half the work, you then need to use that in the build path of your project(s) and potentially in the launch configuration that you use to run your app. As I said, you can't add anything to the build classpath of a project without touching the .classpath, that's how Eclipse stores what the project's compile-time classpath consists of. – E-Riz Oct 28 '13 at 01:00
  • Having said that, if the project needs these JARs to compile, then they must already be specified in the build path somehow; otherwise it would not compile in anyone's Eclipse workspace. Did you mean that the libs are only needed at runtime, not at compile time? If so, you could use the Classpath Variable only in your launch configuration, or use the Web Tools as recommended in one of the answers below. – E-Riz Oct 28 '13 at 01:01
  • I don't understand how everyone else runs the projects because there's nothing that defines their tomcat libs in the project code/classpath. They're all on Windows and I'm their first mac user. They never had to manually add the tomcat libs to eclipse as far as they know. – helion3 Oct 28 '13 at 02:47
  • In order to compile `Servlet`s you need `servlet-api` dependency, and probably the `servlet-jsp-api`. Those are compile-time only dependencies, you don't / shouldn't have to deploy them. But they have to be in the classpath. – Cebence Oct 29 '13 at 14:24

5 Answers5

28

You can install Webtools plugin (installed by default in Eclipse for JavaEE Developers) and then you will get Tomcat runtime support. Then you can add a "Tomcat Runtime" to your project and it will automatically include all Tomcat jars that are available to webapps running on Tomcat.

  1. Window > Preferences > Server > Runtime environments
  2. Add..
  3. Select Tomcat version
  4. Browse to tomcat install dir
  5. Right click you project > Properties
  6. Select Java Build Path
  7. Add library... > Server Runtime > select your runtime you created

This process will also modify your .classpath file but it will do so in a way that is easy to share with other developers.

enter image description here enter image description here

gamerson
  • 5,220
  • 1
  • 28
  • 45
1

Maybe you can create an user library which include the jars in the tomcat/lib, then add the user library in your java projet by those steps :

Build Path > Configure Build Path > Libraries > Add library > Add user library

wang
  • 11
  • 1
0

I use sysdeo tomcat plugin found at http://www.eclipsetotale.com/tomcatPlugin.html. You can also install it by going to Help->Eclipse Marketplace->search for sysdeo

What it does is add a menu item called tomcat in your options that allows you to add projects to tomcat classpath and add tomcat libs to your project pre-compile.

Heres some screenshots of it.

starting and stopping tomcat is made easier: enter image description here

and where you can add the libs to classpath (and much more)

enter image description here

ug_
  • 11,267
  • 2
  • 35
  • 52
  • Eclipse doesn't seem to allow me to install this, maybe it's no longer supported? It shows 0 installs for the past month at least – helion3 Oct 21 '13 at 23:21
  • I have never installed it from the marketplace but you could try to download it right from the website and install it manually – ug_ Oct 22 '13 at 03:12
  • Manually installed this plugin, added the tomcat lib directory to the classpath field as your screenshot recommends. Eclipse still shows red underlines for javax.servlet.http.HttpServletRequest packages, and when I boot the tomcat server through eclipse, I still see ClassNotFoundException: HttpServletRequest. – helion3 Oct 22 '13 at 18:11
0

I've had this problem several times while using subversion with windows users. The Tomcat directory will have servlet-api.jar. Copy and paste it in your eclipse project, right click and add to build path.

Now you don't want to commit the classpath. Then simply deselect .classpath file while committing.

Vikrant Goel
  • 654
  • 6
  • 20
-1

basically i am using windows i dont know about that, but you can do that from eclipse right click on your project in eclipse then see there is a build path from that import(some thing like that) server's(tomcat) lib file where you have install server and put all libs file if you want all

Mohsin AR
  • 2,998
  • 2
  • 24
  • 36
  • I'm not clear on exactly which 'build path' menu choice you're referring to, but I can't add them as external JARs, user libraries, etc because those all change the .classpath file. – helion3 Oct 21 '13 at 23:31
  • 1
    take these steps: `Build Path > Configure Build Path > Libraries > Add External jars` locate the location where you have installed your server like i haved install in `C:` and for server lib i have this folder's path `C:\server\tomcat-7.0.39\lib` then select `servlet-api.jar` and `jsp-api` some thing like that – Mohsin AR Oct 22 '13 at 01:19
  • That method was what I originally was doing but it can't be a long-term solution because it changes the classpath file in the project, which would then show in the version control commits. I cannot commit that file (but can't ignore it either, it's already tracked in version control) – helion3 Oct 22 '13 at 03:14