• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Getting Rid of Redundant Jars.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I determine which all classes and jars my Java program is referencing
from a all the jars in class path ?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>java -verbose:classes [rest of args...]</code>

Important: this will only show the classes that you actually use during while the application is run. It will miss classes that your code uses, but which you don't actually trigger, e.g., for reports.

Another approach is to implement your own ClassLoader that defers to the parent class loader, but which logs which classes are requested. This is more flexible, but won't catch all files.

The other approach is to use reflection to recursively inspect the class files, but that will 1) be hard and 2) pull in a lot of stuff you don't use.
 
Marshal
Posts: 80133
417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For non-simple programs that's nearly impossible. The response above shows how to determine which classes are used during one single invocation; another invocation with other parameters may need other classes because other functionality is used.

Analyzing the code using reflection is also not feasible if advanced features are used (where advanced features start already with stuff like XML and JDBC).

JDBC is a good sample to explain why this causes problems: The old way to configure program with the JDBC driver was to call Class.forName( "JDBCDriverClass" ). Obviously there is no reference to this class in the binary code of that program. And when the name of the JDBCDriverClass is not hardcoded but read from somewhere, the program will never have any static reference of any kind to the jar holding the JDBC driver.

Similar scenarios can be found for XML parsers, Script engines, JNDI providers, Security providers, Encryption providers, Codecs, URL protocols ... I bet I missed a few ...

This also shows that different sessions with a program may use different classes from different jars: just assume a program like Squirrel that can be used to visualize different RDBMS databases.

On the other hand, if you have a simple, straight-forward program that does not use any kind of runtime plug-in mechanism, it could be possible to determine the really necessary jars: when you compile start with an empty CLASSPATH and add the jars until the compiler does not complain any longer.
This can be done also with running the program, but you may get surprised if someone uses a function that was not included into your tests - and now causes a CNF-Exception. But again, this will only work for really straight-forward programs.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For complex projects, one tool I highly recommend is Apache Ivy, which works well with ant build scripts and lets you create nice dependency reports. It does take some time to set up, but once it's done, it can really help reduce jar bloat.
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For complex projects, one tool I highly recommend is Apache Ivy, which works well with ant build scripts and lets you create nice dependency reports. It does take some time to set up, but once it's done, it can really help reduce jar bloat.




Interesting !! I will give it a try.
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic