Custom Class Loaders in Java

Monali Shinde
3 min readNov 30, 2020

--

Previous article explains about the Class Loaders in Java, functionality of Class Loaders and different types of inbuilt Class Loaders. This inbuilt class loaders loads the classes either from bootstrap location or from extension location or from classpath location. There might be some scenarios come where we need to load classes from different locations like out of the local hard drive or a network, in this case we can write our own Custom Class Loader. Custom Class Loaders are helpful for more than just loading the class during runtime, a few use cases might include:

  1. Helping in modifying the existing bytecode, e.g. weaving agents
  2. Creating classes dynamically suited to the user’s needs. e.g in JDBC, switching between different driver implementations is done through dynamic class loading.
  3. Implementing a class versioning mechanism while loading different bytecodes for classes with same names and packages. This can be done either through URL class loader (load jars via URLs) or custom class loaders.
  4. Browsers, for instance, use a custom class loader to load executable content from a website.
  5. Developers have two well-known reasons for building custom ClassLoaders: providing support for a new class repository and partitioning user code in a server. The applet ClassLoader is an example of the former. It introduces the ability to load classes from an HTTP server. The latter purpose -- partitioning user code in a server -- is less frequent. For example, consider a servlet engine responsible for running servlets created by several developers. Those developers may be unaware of each other, as in a commercial Web-hosting environment, and they will want their Java code to execute independently of any other code using the servlet runner. A custom ClassLoader can maintain a separate ClassLoader for each developer's servlet. Since a class is identified in a Java virtual machine (JVM) by its full name and the ClassLoader that loaded it, classes loaded by a different ClassLoader are effectively separated. This use of custom ClassLoaders provides for multiple pseudoapplications to run concurrently in the JVM.

How to create Custom Class Loader in Java?

  1. Create a class that will extend ClassLoader.
  2. Override the method findClass().
  3. Create a method that will load your given class from the given location. That created the method will return byte[]. This byte array will be returned as Class by defineClass() method.

A delegation design was put in place for java.lang.ClassLoader. In the delegation design, a Custom ClassLoader delegates class loading to its parent. A ClassLoader parent can be either the Bootstrap ClassLoader or another Custom ClassLoader. In the event the parent ClassLoader can't load a class, a new method, named findClass(), is called on the ClassLoader subclass. In this manner, the Custom ClassLoader is responsible for loading only the classes not available to the parent -- presumably classes that come from a new type of class repository.

Please refer example for custom class loader https://github.com/MonaliS/JavaCustomClassLoader

--

--