RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Minimize Java Distributions with an Automated, Custom JAR File

Larger applications frequently get loaded down with a heavy burden of unused library files. Learn to create a custom classloader that automatically builds a JAR file that contains only the classes you need.

his article presents a solution to a problem that you might not have known you had—the 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 programs—especially one that used a set of evolving libraries—you 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.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date