How has class loading and class loaders changed in recent Java versions?

In recent Java versions, class loading and class loaders have undergone significant changes aimed at improving performance, security, and modularity. One of the most notable changes is the introduction of the Java Platform Module System (JPMS) in Java 9, which reorganizes how classes and modules are loaded and managed. This system promotes better encapsulation, allowing developers to define explicit dependencies between modules and making it easier to manage large codebases. Additionally, enhancements to the dynamic class loading capabilities have improved runtime behavior and flexibility.

Furthermore, the class loader architecture has evolved, with new APIs introduced to support more sophisticated use cases, such as application class loaders that can be customized to load classes from various sources. These changes also enhance the security aspects of class loading, allowing for finer-grained control over the classes that can be loaded and executed.

Example of Custom Class Loader

public class CustomClassLoader extends ClassLoader { @Override protected Class> findClass(String name) throws ClassNotFoundException { byte[] b = ... // Load class data as byte array return defineClass(name, b, 0, b.length); } }

class loading class loaders Java 9 JPMS custom class loader