views:

43

answers:

4

I developing a web application with a lot of libraries like, Spring, Apache CXF, Hibernate, Apache Axis, Apache Common and so one. Each of these framework comes with a lot of *.jar libraries.

For development I simple take all of the delivered libraries and add them to my classpath.

For deployment not all of these libraries are required, so is there a quick way to examine all the required libraries (*.jar) which are used by my source code?

A: 

JDepend traverses Java class file directories and generates design quality metrics for each Java package. JDepend allows you to automatically measure the quality of a design in terms of its extensibility, reusability, and maintainability to manage package dependencies effectively.

Bakkal
+3  A: 

If you move your project to use Maven such things become easier:

mvn dependency:analyze 
mvn dependency:tree
spektom
Out of curiosity, how does Maven resolve dependencies stemming from programmatic loading of classes? I'm guessing it doesn't. Not to detract from your answer- I'm sure its a great time saver. I'm just saying...there's no real substitute for reading the docs of the third-party libraries you're using.
+1  A: 

For your example, Maven + IDE + nice dependency diagrams could help allot.

See an example of this : it's much easier this way to figure out what happens in a project, and this way you don't need to add to your project "all delivered libraries" - just what it's required.

Adrian A.
A: 

So, as a quick, dirty, and potentially inefficient way, you can try this in Eclipse:

  1. Create two copies of your project.
  2. In project copy #2 remove all the jars from the classpath.
  3. Pick a source file that now has errors because it can't resolve a class reference. Pick one of the unresolved classes and note its fully qualified class name.
  4. Do Control-Shift-T and locate the unresolved class. You should be able to see which jar its contained in since all the jars are still in the classpath for project copy #1.
  5. Add the jar that contains this unresolved class back into your classpath in project copy #2, then repeat steps 3 and 4 until all class references are resolved.

Unfortunately you're not done yet since the jar files themselves may also have dependencies. Two ways to deal with this:

  1. Go read the documentation for all the third-party packages you're using. Each package should tell you what its dependencies are.

  2. Run your application and see if you get any ClassNotFoundExceptions. If you do, then use Control-Shift-T to figure out what jar that class comes from and add it to your classpath. Repeat until your project runs without throwing any ClassNotFoundExceptions.

The problem with #2 is that you don't really know you've resolved all the dependencies since you can't simulate every possible execution path your project might take.