views:

279

answers:

2

I have several J2EE projects using servlets, each of which extends from a base class that handles the main doGet and doPost methods and calls more specific methods on the subclasses. Rather than have that base class as a class in each WAR file, I want to move it to its own JAR that each of the other projects can reference.

I'm using Eclipse. So, I created a new Java project, and moved the base class into there. It complained about not having a reference to servlet-api.jar, so I added it there. Then the other projects complained about not having a reference to that new project, so those references were added.

Now, there's no more complaining, but when I run the project (by opening the servlet class and choosing Run As), it doesn't export the classes from the other project and Tomcat throws an exception when trying to instantiate a subclass of an unknown class.

How do I tell eclipse to export both the servlet project itself and this other java project that it's referencing?

A: 

Check the CLASSPATH setting of the runtime configuration. Instead of relying on 'Run As...' go ahead and create an explicit runtime config for your servlets. That way you can make sure to include any dependent classes in projects other than the one being run.

Kelly French
+1  A: 

I'm using Eclipse. So, I created a new Java project, and moved the base class into there. It complained about not having a reference to servlet-api.jar, so I added it there.

Ensure that you added it by Java Build Path > Libraries > Add Library > Server Runtimes > [select the server in question] (and thus not copied or imported, else the classpath may collide with those of the real used server).

Then the other projects complained about not having a reference to that new project, so those references were added.

I'm not sure if I understand the root cause here.

Now, there's no more complaining, but when I run the project (by opening the servlet class and choosing Run As), it doesn't export the classes from the other project and Tomcat throws an exception when trying to instantiate a subclass of an unknown class.

You need to configure the deployed web project to export the project references as well. Go to Java EE Module Dependencies in project properties and tick the projects which are to be exported as dependency.

BalusC
There is a bit of a hitch with that last step. There are no projects listed in that area, and the only option I have is to add JAR files, which don't exists in the library project.
Ed Marty
You need to add those projects first by *Java Build Path > Projects > Add.* Also see the 2nd part of my answer.
BalusC
I had already recreated the project as a dynamic web project and added it to the main project as part of the build path.
Ed Marty
I was wrong (I didn't realize that you cannot select Dynamic Web projects as Java EE Module Dependency), I updated the answer. Just have a normal Java project, add Server Runtime libraries to it, then in the web project add it to Java Build Path and tick it in Java EE Module Dependencies.
BalusC
Totally worked. Awesome
Ed Marty
You're welcome.
BalusC