Find unused classes in a Java Eclipse project

JavaEclipseRefactoring

Java Problem Overview


I have a large Eclipse project in which there exist several classes which, although they ceased to be used anywhere, were never marked @Deprecated.

How can I easily find all of these?

Java Solutions


Solution 1 - Java

I also like to use UCDetector:

screenshot

> UCDetector (Unecessary Code Detector) is a Open Source eclipse PlugIn Tool to find unecessary (dead) public java code. It also tries to make code final, protected or private.

Bonus: it can also find cyclic dependencies between classes

(also a number of other tools -- including Findbugs -- knows how do do that too)


Caveat: Cid mentions in the comments:

> UCDetector shall not work if there are interface implementations which will be known only at runtime.
It incorrectly marks the implementation classes as unused.


Update 2017: static code analysis has evolved quite a bit in 8 years.
Using SonarLint for Eclipse, you can use the the latest SonarJava 4.6 plugin to analyze your code.
It will find dead code.

Solution 2 - Java

ProGuard can be used to print a report of unused classes/methods. It's a pain to supply all the dependent jars to it, though.

These options list unused classes, fields, and methods in the application mypackage.MyApplication:

-injars      in.jar
-libraryjars <java.home>/lib/rt.jar

-dontoptimize
-dontobfuscate
-dontpreverify
-printusage

-keep public class mypackage.MyApplication {
    public static void main(java.lang.String[]);
}

Solution 3 - Java

Just use Analyze | Inspect Code with appropriate inspection enabled (Unused declaration under Declaration redundancy group).

Using IntelliJ 11 CE you can now "Analyze | Run Inspection by Name ... | Unused declaration"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjjujumaView Question on Stackoverflow
Solution 1 - JavaVonCView Answer on Stackoverflow
Solution 2 - JavarustyxView Answer on Stackoverflow
Solution 3 - JavaHasan JamshaidView Answer on Stackoverflow