his article presents a solution to a problem that you might not have known you hadthe size of your Java code.
Java programs can run the gamut from very small to very large. In fact, the variance is much larger in Java than in any other language. This is partly because the Java runtime (JRE) contains a great deal of library code that doesn't need to be included in an application, allowing applications to be smaller than they otherwise would.
However, if you've ever worked on a suite of Java programsespecially one that used a set of evolving librariesyou know that Java programs can grow extremely large. Java encourages modular design and code reuse, and it's not uncommon to find yourself including code you've never seen before into a bundled application, simply because it's part of your company's library set.
In such cases, you might be tempted to remove some of the dead code, but this isn't something you want to do by hand, and the larger and more complex the program, the more reluctant you should be to attempt this.
Classloader to the Rescue
Fortunately, Java provides a solution. You can use the power of Java Classloaders to track the code that your application uses. More specifically, you can get a very precise idea of which classes you are using and which you aren't.
In this article, I'll develop a utility that will run a program under a monitor. This monitor will track the classes that are loaded, and when the program is done, it will create a JAR file containing only the classes you need. I call this program 'JarMaker.' Along the way, you'll learn a lot about the Java Classloader. I'll start there, with the basics of the Java Classloader.