Is there any tool that lists which and when some classes are effectively used by an app or, even-better, automatically trims JAR libraries to only provide classes that are both referenced and used?
4 Answers
Bear in mind that, as proven by the halting problem, you can't definitely say that a particular class is or isn't used. At least on any moderately complex application. That's because classes aren't just bound at compile-time but can be loaded:
- based on XML config (eg Spring);
- loaded from properties files (eg JDBC driver name);
- added dynamically with annotations;
- loaded as a result of external input (eg user input, data from a database or remote procedure call);
- etc.
So just looking at source code isn't enough. That being said, any reasonable IDE will provide you with dependency analysis tools. IntelliJ certainly does.
What you really need is runtime instrumentation on what your application is doing but even that isn't guaranteed. After all, a particular code path might come up one in 10 million runs due to a weird combination of inputs so you can't be guaranteed that you're covered.
Tools like this do have some value though. You might want to look at something like Emma. Profilers like Yourkit can give you a code dump that you can do an analysis on too (although that won't pick up transient objects terribly well).
Personally I find little value beyond what the IDE will tell you: removing unused JARs. Going more granular than that is just asking for trouble for little to no gain.

- 616,129
- 168
- 910
- 942
-
1What if we do not load classes dynamically? In other words, no XML config, properties files, annotations, rpc, and whatnot. Then can such a tool be *guaranteed* to work? – Pacerier Jun 13 '14 at 11:02
-
The Halting Problem only rules out guaranteed correct yes/no answers to "non-trivial" properties. We can avoid the HP by allowing incorrect answers (false positives/negatives); or by allowing an extra "unknown" answer; or by making it "trivial" (e.g. whether certain keywords appear anywhere in the program) – Warbo Oct 30 '20 at 14:42
Yes, you want ProGuard. It's a completely free Java code shrinker and obfuscator. It's easy to configure, fast and effective.

- 63,018
- 25
- 139
- 189
-
2Just make sure you provide correct list of 'root' classes, from which references will be searched, and that you don't forget any class which you use by reflection. – Peter Štibraný Jan 24 '09 at 10:06
You might try JarJar http://code.google.com/p/jarjar/
It trims the jar dependencies.

- 1,600
- 2
- 16
- 25
-
Specifically create a fat jar and a jar of just your code. Using 'java -jar jarjar.jar find class code.jar fat.jar' which will list your dependencies. You can create a rules file of "keep" statements based on this and then process your fat jar into your minimized jar. – Lanny Ripple Dec 09 '15 at 17:13
For most cases, you can do it quite easily using just javac.
Delete you existing class files. Call javac with the name of your entry classes. It will compile those classes necessary, but no more. Job done.

- 145,806
- 30
- 211
- 305