69

Are there any plugins/tools available to go through the classpath of an eclipse project (or workspace) and highlight any unused jars?

6 Answers 6

48

ClassPathHelper is a good start.

It automatically identifies orphan jars and much more.

The only limitation is with dependencies that are not defined in classes, e.g. in dependency injection framework configuration files.

You also have other options/complements, such as:

  • workingfrog "Relief", which relies on the ability to deal with real objects by examining their shape, size or relative place in space it gives a "physical" view on java packages, types and fields and their relationships, making them easier to handle.
  • Unnecessary Code Detector: a eclipse PlugIn tool to find unnecessary (dead) public java code.
6
  • Similar question with a different answer stackoverflow.com/questions/1012298/…
    – Kunal Jha
    Jul 26, 2013 at 4:44
  • the first link @VonC mentioned seems moved/broken
    – manocha_ak
    Oct 24, 2013 at 6:52
  • @manocha_ak yes, the Relief project on workingfrog is no more. I have restored a web.archive.org link to that site.
    – VonC
    Oct 24, 2013 at 7:37
  • Still cannot get that. Can you send me over individually or some other way?
    – manocha_ak
    Dec 4, 2013 at 12:34
  • 1
    @manocha_ak I understand, but only the web page has been preserved, not the archive. As far as I know, that project is gone.
    – VonC
    Dec 4, 2013 at 12:36
32

UCDetector does not help for this : It does not work on JARs. And for classpathHelper, I wan't able to find out an easy way just to list the orphan JARs (BTW, if someone has a tutorial for this, i am interested).

So, if you are also using Maven as I do, I find out this great Maven plugin, and I would like to share this solution with you. Just type :

mvn dependency:analyze

And you will instantly get a list of unused JARs in your dependencies. Very handy !

2
  • 3
    Thanks a lot for the tip of mvn dependency:analyze. It does the job in a very easy way. In my opinion, much simpler than using one of the other tools mentioned. Nov 13, 2011 at 19:21
  • 1
    Be aware that mvn dependency:analyze could give false negatives: for example it cannot discover a dependency if the dependent class is instantiated through Class.newInstance() or Constructor.newInstance() and manipulated through an interface. This is a common scenario in projects that use a dependency injection framework like Spring. However it's a good starting point.
    – Pino
    Aug 25, 2014 at 15:17
10

I found a very fast and interesting tool to archive this goal:

http://tattletale.jboss.org/

Just unzip the program and run:

java -Xmx512m -jar tattletale.jar ~/myjavaproject/mydistribution output

This will generate a very impressive report with different points (text from their site):

  • Identify dependencies between JAR files
  • Find missing classes from the classpath
  • Spot if a class/package is located in multiple JAR files
  • Spot if the same JAR file is located in multiple locations
  • With a list of what each JAR file requires and provides
  • Verify the SerialVersionUID of a class
  • Find similar JAR files that have different version numbers
  • Find JAR files without a version number
  • Find unused JAR archives
  • Identify sealed / signed JAR archives
  • Locate a class in a JAR file
  • Get the OSGi status of your project
  • Remove black listed API usage
  • And generate the same reports for your .WAR and .EAR archives
3
  • 1
    Thanks. It seems to work fine. It needs to be called on compiled .jars though, not on .java files. Dec 6, 2018 at 9:25
  • 1
    I tried this, installed, it worked, but it did not tell me which jars are unnecessary. It created a page for this, but it listed just the main WAR file for the entire application which is clearly "wrong" and it did not find the unnecessary JAR within the application.
    – AgilePro
    Jan 2, 2022 at 23:17
  • @AgilePro What worked for me was extracting all ear/war files so everything is just jars. Nov 1, 2022 at 17:43
7

You can use one of this plugins: UCDetector or Classpath Helper

0
0

I know this is an old one, but if anyone else stumbles upon this, Eclipse does this by itself.

Navigate to Project properties->Java Code Style->Clean Up Select the Eclipse [Built-in] and it does the following:

  • Change non static accesses to static members using declaring type
  • Change indirect accesses to static members to direct accesses (accesses through subtypes)
  • Remove unused imports
  • Add missing '@Override' annotations
  • Add missing '@Override' annotations to implementations of interface methods
  • Add missing '@Deprecated' annotations
  • Remove unnecessary casts
  • Remove unnecessary '$NON-NLS$' tags
1
  • This isn't quite what I was asking. I was wanting to find which jars are included in the project that aren't necessary. Cleaning up does a different (but still useful) job. Cheers!
    – RodeoClown
    Feb 12, 2013 at 3:05
0

In the Maven and Gradle projects you can use those plugins to identify unused dependencies.

Use this in the pom.xml file plugin.

<build>
<plugins>
    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.1.2</version>
    </plugin>
</plugins>
</build>

this will give as output. In this case we add commons-collections dependency to pom.xml, but do not use in the code. enter image description here

Use this in the build.gradle file plugin.

plugins {
  id "ca.cutterslade.analyze" version "1.7.1"
}

Using legacy plugin application:

enter image description here

this will give as output. In this case we add dependencies unusedDeclaredArtifacts to gradlefile, but do not use in the code. enter image description here

1
  • Please add short examples of how to do it, if possible, in case the links break in the future.
    – MC Emperor
    Jul 22, 2021 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.