views:

76

answers:

2

How would you determine the classes (non Sun JDK classes) loaded / unused by a Java application?

Background:
I have an legacy Java webstart application that has gone through a lot of code changes and now has a lot of classes, most of which are not used. I would like to reduce the download size of the application by only deploying classes that will be used only instead of jaring the all the packages.

I will also use the same process to completely delete these unused classes.

+1  A: 

You can use a good IDE for that.

For instance Intellij IDEA which analyzes the source code for dependencies and allows you to safely delete a class/method/attribute is is not being used by any other.

That way you can get rid off all your dead code.

OscarRyz
Same goes for NetBeans with the safe delete concept, you can right-click and choose "Find Usages" on anything from variables to classes to find out exactly where it is used (or if it is not used at all).
Kavon Farvardin
anything for eclipse?
n002213f
@n002213f : There must be. I bet it is very well hidden in one of those multiple "perspectives" http://stackoverflow.com/questions/238177/worst-ui-youve-ever-used/568142#568142
OscarRyz
@Oscar Reyes - nice one, LOL
n002213f
@Kavon Farvardin - and i do that for all the classes in the project, there must be a better way
n002213f
Right click on the identifier; it's in the contextual menu
Steven Schlansker
@ Steven Schlansker - please elaborate
n002213f
+2  A: 

Use java -verbose:class to see what classes are loaded, then use grep (or any other tool) to keep only the lines from your packages.

A small limitation: it will only tell you which classes are really used when they are used, so you must cover all use cases of your application.

Jerome
Although this is a good option you have to make sure you "touch" every single part of your application to have that class loaded. Otherwise you may end up not considering classes but just because you didn't play with them.
OscarRyz
You're right, I'm adding this fact in my answer.
Jerome
+1, just what i needed, thanks
n002213f