Ed Marty
Ed Marty

Reputation: 39690

How do I reference and export another project when developing a J2EE web app?

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?

Upvotes: 3

Views: 8438

Answers (2)

BalusC
BalusC

Reputation: 1108557

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.

Upvotes: 4

Kelly S. French
Kelly S. French

Reputation: 12334

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.

Upvotes: 0

Related Questions