9

I have a Java project that currently has a lot of JARs in its libraries directory, which are all included in the resulting package when building. I know, however, that some of these libs are never referenced in the project.

Is there a tool that can search for libs that are not referenced within the project? I guess there must be something in that sense.

BTW, an Eclipse plugin would be awesome.

EDIT: I chose to go with ClassDep because it was the only suggestion that worked. However, I'm having some trouble with it: please check this question

Community
  • 1
  • 1
André Chalella
  • 13,788
  • 10
  • 54
  • 62
  • Excellent question -- I've always wanted a tool to do this. There's gotta be one out there! – William Brendel Apr 01 '09 at 20:25
  • http://sourceforge.net/project/downloading.php?group_id=147285&use_mirror=freefr&filename=cphelper_1.2.8GA.zip does work for download – VonC Apr 02 '09 at 13:23
  • I will share it tonight or tomorrow: any sharing file is in "access denied" from work ;) – VonC Apr 02 '09 at 15:11
  • If you leave here your email in the next 10 minutes, I can try and share it through http://www.quickeo.com/about-quickeo.html – VonC Apr 02 '09 at 15:29
  • Nope, does not work either... So tonight it is. – VonC Apr 02 '09 at 15:32
  • Shared: http://rapidshare.de/files/46493178/cphelper_1.2.8GA.zip.html – VonC Apr 02 '09 at 20:17

6 Answers6

8

Beware of the case that a class is loaded via Class.forName() and not specified as a dependency in the manifest file (there is a Depends-On: attribute that is for that, but many people don't specify it, which breaks tools like this, the bane of my existence when I worked on such a tool).

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
4

ClassDep (from Sun, in the Jini development kit) will do this for you.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
2

ClassPathHelper can help you with that.

Espacially the "Not on Classpath View"

Not on Classpath

This view scans for jars that are not on the classpath (but are under the current project). It provides basic browsing of packages and classes that are available but not on the classpath. This can be helpful when trying to build up a classpath, as you can quickly browse for the missing classes to see which jars contain them.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I must hang out here too much... When I saw an Eclipse screenshot, I immediately thought "VonC!" – Michael Myers Apr 01 '09 at 20:38
  • TssTss... and yet another entry to add to the "You know you’ve been browsing Stack Overflow too much when?" question ( http://stackoverflow.com/questions/247342 ) ;) – VonC Apr 01 '09 at 21:22
  • I'm dying to test it, but can't download it! http://sourceforge.net/project/showfiles.php?group_id=147285&package_id=162288 (says it's unavailable - WTF?) – André Chalella Apr 02 '09 at 13:19
  • http://sourceforge.net/project/downloading.php?group_id=147285&use_mirror=freefr&filename=cphelper_1.2.8GA.zip does work – VonC Apr 02 '09 at 13:23
  • probably a mirror issue. Try my link or try to switch between different mirrors. – VonC Apr 02 '09 at 13:29
  • Your link didn't work here either, plus I can't get to see a list of mirrors to choose from (just autodetect). – André Chalella Apr 02 '09 at 13:35
  • Strange. Since I can download it (behind a corporate HTTP proxy), I suppose you may have a problem with your Internet access configuration. The file is certainly available. – VonC Apr 02 '09 at 13:37
  • I asked a bunch of friends from different places in town to test the link and it still fails to work. Certainly not a problem here, and I'd really like to test the app before leaving for tomorrow. Any chances of uploading the bins somewhere? :) <- nice smile – André Chalella Apr 02 '09 at 13:49
1

I wrote a small eclipse plugin that takes an existing java project from the workspace. For every classpath entry of the projects raw classpath it removes it from the projects raw classpath and builds the project. If no problem markers with severity error appear on the project, it permanently removes the classpath entry from projects raw classpath.

I'm not able to share that plugin, but that is not too much work to implement it yourself with the links to the api given above.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
1

Not an eclipse plugin, but I believe the "shrinking" functionality of ProGuard is exactly what you're looking for.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • I don't think so: proguard can remove unused classes from your input jars but does not remove library jars from a project – Maurice Perry Apr 01 '09 at 20:40
0

You also can't tell if JARs that you don't import are required dependencies of dependencies. For example, if you use Spring it comes with its own dependencies, even if you don't import or call those classes in your code. I'm ignorant of ProGuard - does it check for those cases?

duffymo
  • 305,152
  • 44
  • 369
  • 561