tags:

views:

20

answers:

2

In Eclipse Java EE perspective, how does one add a thridparty Jar to a Utility Project?

To elaborate: In a "normal" Java (Not Java EE) project, there's Referenced Libraries where you can put jars. In a Dynamic Web Project, there's Web App Libraries. In a Utility Project, there's only EAR Libraries, which don't appear relevant (well, there are Referenced Libraries that show up in the Package Explorer in Java perspective, but not in the Project Explorer in the Java EE perspective). I went ahead and added a /lib directory under my Utility Project root, and put a jar there (I forgot if I did that in the Java Package perspective, or just in the file system). I added it to Java Build Path, and everything compiles, including the Dynamic Web projects that reference the Utility Project. But when I deploy to Tomcat, I get ClassNotFoundException for the classes in the thirdparty jar.

How do I add the thirdparty jar to the Utility Project in a way that will make it get deployed as part of the web application?

A: 

I am not sure to understand exactly your question, but you can simply right-click on your project, in Properties you select Java Build path > Libraries, and then add your JAR in the list of third parties libraries. This will add your jar in the classpath project.

Another way is to use the (ugly) solution of creating a lib directory, put all your third parties libraries in it, and add this directory in the classpath of your project (like for the previous solution, as you can add a whole directory in Eclipse project).

The last solution is to give the responsibility of the dependencies management to a real build system, such as Maven, Ant+Ivy, Gradle...

romaintaz
+1  A: 

I answered something similar before.

http://stackoverflow.com/questions/3886530/eclipse-and-how-it-handles-jars-odd-case/3886735#3886735

In essence, there's a difference between build-time and run-time JAR dependencies. For inclusion in EAR / WAR files, you have to use the "Deployment Assembly" panels.

Isaac
That seems like the right path, but I don't see Deployment Assembly panel. I'm on Eclipse 3.5...
ykaganovich
Ok, I upgraded to Helios using http://wiki.eclipse.org/FAQ_How_do_I_upgrade_Eclipse%3F and can ad the thirdparty jars from the Utility Project to the Dynamic Web Project Deployment Assembly. I'd prefer if the Dynamic Web Projects picked up the settings automatically from the Utility Project, is that possible? Otherwise, this solution works, I just have to mess with Deployment Assembly settings for **each** Dynamic Web Project that references the Utility Project...
ykaganovich
+1, good question. To my knowledge, this is not implemented in WTP/Eclipse and I think it's for a good reason. For the Deployment Assembly, Eclipse needs to know the *runtime* classpath of the utility module; this is _not necessarily_ identical to the build-time classpath. For example: lets say that your utility project needs Servlet interfaces to compile against. You'd add the required JAR to your utility's buildpath, so it would compile. Great. But would it make sense to add the same JAR to the webapp? definitely and absolutely _not_, because the JAR is provided by the webapp's runtime.
Isaac