ArticleslgStudy

science

Java class loader

Java class loader is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Java class loader rather than just read about it. In short: The Java class loader, part of the Java Runtime Environment, dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand.

Key takeaways

  • Java class loader belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Java class loader to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Java class loader from memory before moving on to harder problems.

Reference excerpt

The Java class loader, part of the Java Runtime Environment, dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The virtual machine will only load the class files required for executing the program. The Java run time system does not need to know about files and file systems as this is delegated to the class loader. A software library is a collection of related object code. In the Java language, libraries are typically packaged in JAR files. Libraries can contain objects of different types. The most important type of object contained in a Jar file is a Java class. A class can be thought of as a named unit of code. The class loader is responsible for locating libraries, reading their contents, and loading the classes contained within the libraries. This loading is typically done "on demand", in that it does not occur until the class is called by the program. A class with a given name can only be loaded once by a given class loader. Each Java class must be loaded by a class loader. Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries. When the JVM is started, three class loaders are used:

Bootstrap class loader Extensions class loader System class loader The bootstrap class loader loads the core Java libraries located in the <JAVA_HOME>/jre/lib (or <JAVA_HOME>/jmods> for Java 9 and above) directory. This class loader, which is part of the core JVM, is written in native code. The bootstrap class loader is not associated with any java.lang.ClassLoader object. For instance, StringBuilder.class.getClassLoader() returns null. The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property). The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable.

User-defined class loaders The Java class loader is written in Java. It is therefore possible to create a custom class loader without understanding the finer details of the Java Virtual Machine. Apart from the Bootstrap class loader, every Java class loader has a parent class loader. The parent class loader is defined when a new class loader is instantiated or set to the virtual machine's system default class loader. This makes it possible (for example):

to load or unload classes at runtime (for example to load libraries dynamically at runtime, even from an HTTP resource). This is an important feature for: implementing scripting languages, such as Jython using bean builders allowing user-defined extensibility allowing multiple namespaces to communicate. This is one of the foundations of CORBA / RMI protocols for example. to change the way the bytecode is loaded (for example, it is possible to use encrypted Java class bytecode). to modify the loaded bytecode (for example, for load-time weaving of aspects when using aspect-oriented programming).

Class loaders in Jakarta EE Jakarta EE (formerly Java EE and J2EE) application servers typically load classes from a deployed WAR or EAR archive by a tree of class loaders, isolating the application from other applications, but sharing classes between deployed modules. So-called "servlet containers" are typically implemented in terms of multiple class loaders.

JAR hell JAR hell is a term similar to DLL hell used to describe all the various ways in which the classloading process can end up not working. Three ways JAR hell can occur are:

Accidental presence of two different versions of a library installed on a system. This will not be considered an error by the system. Rather, the system will load classes from one or the other library. Adding the new library to the list of available libraries instead of replacing it may result in the application still behaving as though the old library is in use, which it may well be. Multiple libraries or applications require different versions of library foo. If versions of library foo use the same class names, there is no way to load the versions of library foo with the same class loader. The most complex JAR hell problems arise in circumstances that take advantage of the full complexity of the classloading system. A Java program is not required to use only a single "flat" class loader, but instead may be composed of several (potentially very many) nested, cooperating class loaders. Classes loaded by different class loaders may interact in complex ways not fully comprehended by a developer, leading to errors or bugs that are difficult to analyze, explain, and resolve. The OSGi Alliance specified (starting as JSR 8 in 1998) a modularity framework that aims to solve JAR hell for current and future VMs in ME, SE, and EE that is widely adopted. Using metadata in the JAR manifest, JAR files (called bundles) are wired on a per-package basis. Bundles can export packages, import packages and keep packages private, providing the basic constructs of modularity and versioned dependency management. Java 9 introduced the Java Platform Module System in 2017. This specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or modules, and identifies how they can be discovered, loaded and checked for integrity. It includes features such as namespaces with the aim of fixing some of the shortcomings in the existing JAR format. The Java Platform Module System follows a different philosophy from the OSGi architecture that aims at providing modularity for the Java Runtime Environment in a backwards-compatible way that uses the default mechanism of loading classes that the JRE provides. However, since the Java Platform Module System does not offer the ability for controlled co-existence of libraries with different versions, it does not fully address the JAR hell problem.

See also Loader (computing) Dynamic loading DLL hell OSGi Classpath (Java) Java Platform Module System

Footnotes

References

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Java class loader

Start with the simplest possible case. Write down what Java class loader claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Java class loader before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Java class loader ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Java class loader

In research
Java class loader appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Java class loader in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Java class loader is common in secondary-school and first-year university syllabi. It links to neighbouring topics Java (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Java class loader outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Java class loader” →

How to study Java class loader in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Java class loader means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Java class loader out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Java class loader in simple terms?

The Java class loader, part of the Java Runtime Environment, dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand.

Why does Java class loader matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Java class loader?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Java class loader.

Tags

  • Java (programming language)

Keep exploring