Browse DevX
Sign up for e-mail newsletters from DevX


Minimize Java Distributions with an Automated, Custom JAR File : Page 2

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Classloader Concept
Java programs are made of individual components called classes. From the point of view of the everyday programmer, a class is a collection of Java code—class declarations, methods, and so on. This is the language-oriented view of a class.

At the same time, a class is a collection of bytes stored on disk somewhere, which must be loaded before it can be run. This is the runtime-oriented view of a class.

A Classloader is an object that converts a class from a collection of bytes into a Java class and is generally responsible for reading those bytes from a disk or some other permanent storage medium. A Classloader, thus, crosses the boundary between the language-oriented view and the runtime-oriented view of what a class is. Working with Classloaders requires you to understand the guts of the runtime environment, as well as other issues that you generally don't have to deal with.

The System Classloader
Fortunately, you don't normally have to worry about Classloaders. When you run your Java program, the JRE automatically creates a Classloader and uses it to load all the classes from disk. Or in the case of an applet, the browser's JRE automatically creates a Classloader that loads the classes over the network. You (and your program) are none the wiser.

The default Classloader that the JRE provides is called the system Classloader, and its job is to load classes from disk, searching directories and JAR files in the user's path to find the necessary classes.

Custom Classloaders
Just as a browser has a special Classloader that loads classes from the network, you can create a custom Classloader to deal with your own special circumstances. For example, we want to load classes from disk as usual, but we want to track which classes are loaded while we do that. For that, we need a custom Classloader.

A Classloader is actually pretty simple to understand. When you create a custom Classloader, you create a class that extends Classloader and you implement one method:

public class JMClassLoader extends ClassLoader { public Class loadClass( String name, boolean resolve ) { // ... } }

The loadClass() method has the burden of loading a class. The system passes loadClass() the name of the class, and loadClass() must return a Class object.

Any Classloader—regular or custom—is used by first asking it for a class, like this:

Class theClass = Class.forName( name, true, jmcl );

Class.forName() is the standard method for loading a class, but we're using the version that takes a Classloader as an argument. This way, you tell the system that you want to load the class using a particular Classloader.

Once you've done that, if this class requires any other classes, it uses the same Classloader to load them. If you load the first class in an application using a custom Classloader, then the entire application will loaded through that Classloader.

Our Custom Classloader
The custom Classloader for this project is called JMClassLoader, where JM stands for 'JarMaker.' Remember, you want to create a ClassLoader that tracks the classes that are loaded and creates a JAR file containing only those classes.

Listing 1 contains the code for JMClassLoader.loadClass(). The line-by-line comments explain how it works.

The most important thing to understand about this Classloader is that it doesn't do anything strange in the actual loading of the classes. To load a class, it simply looks in the current directory (or in subdirectories of the current directory). What makes this Classloader different is that, after it loads the class, it remembers that it loaded it by storing a reference to it in a collection called loadedClasses. Later, it uses this set to generate the JAR file.

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