score:46

Accepted answer

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.

score: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

score: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

score:7

you can use one of this plugins: ucdetector or classpath helper

score:9

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

score: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 !


Related Query

More Query from same tag