Class loading
28 questions in 10 subtopics.
Class loader hierarchy
Describe the built-in class loaders in Java 9 and later: the bootstrap, platform and application (system) loaders. Which classes does each one load, and what doesString.class.getClassLoader() return?A service logs java.lang.ClassCastException: com.acme.Order cannot be cast to com.acme.Order. The class names are identical. What does this mean and how do you find the cause?A Spring Boot application packaged as an executable fat jar runs with java -jar app.jar. A third-party library calls ClassLoader.getSystemClassLoader().getResource("config/rules.xml") and gets null, although the file is in src/main/resources. The same code works when run from the IDE. Why, and how do you fix it?Your company runs about 300 Spring Boot services. Several incidents came from duplicate classes on the class path: two versions of the same library, or one library shaded under its original package name. What standards and build checks would you introduce across teams?Java 9 introduced the module system, and JDK 16 and 17 strongly encapsulated JDK internals so illegal reflective access is denied. Looking back, why was the flat class path a long-term problem, and how should a large organisation decide whether to adopt JPMS modules for its own applications?Parent delegation
Explain parent-first delegation inClassLoader.loadClass. Why can an application not define its own java.lang.String and have it used?Servlet containers such as Tomcat load web application classes child-first (parent-last). Explain why, what they still load from the parent first, and one bug that this model can cause.Custom class loaders
Write a minimal class loader that loads plugin classes from a directory of .class files, and explain which method you override and why. The host application must be able to use plugins through aPluginApi interface.Design plugin isolation for a Java platform where many teams ship plugins into one long-running JVM. Compare a class loader per plugin, JPMS module layers (ModuleLayer), and running plugins out of process.Application servers in the 2000s hosted many applications in one JVM, isolated by per-application class loaders. Today most companies run one application per JVM or container. Explain why this shift happened and what it means when a team now proposes in-process plugin or multi-tenant hosting.Class initialization
What does this program print, and why?class A {
static {
System.out.println("A init");
}
static final int X = 10;
static int Y = 20;
}
public class Main {
public static void main(String[] a) {
System.out.println(A.X);
System.out.println(A.Y);
}
}
java.lang.NoClassDefFoundError: Could not initialize class com.acme.Config, but the class is definitely in the jar. What happened, and where do you look?A service hangs during startup. A thread dump shows thread T1 executing the static initializer of class A and blocked on class B's initialisation, while T2 executes B's static initializer and is blocked on A. Explain the mechanism and how to fix it.Java's lazy class loading and initialisation suited dynamic frameworks but is a large part of startup cost. GraalVM native image can initialise some classes at build time, and the JDK's AOT cache stores loaded and linked classes. What risks does moving initialisation work earlier carry, and what coding policy should an organisation set so it can use these techniques safely over the years?Class unloading
Can you unload a single class in Java, for example a plugin class you no longer need? What has to happen before the JVM can free it?When can the JVM unload a class, and why are classes loaded by the application class loader effectively never unloaded?Metaspace leaks
Name three ways an application can keep creating new classes at run time and grow metaspace without any redeploys, and how you would notice it.After about 30 redeploys of a WAR in a long-running Tomcat without a restart, the JVM fails withOutOfMemoryError: Metaspace. Describe how you find what is holding the old web application class loaders.Hot reloading
How does Spring Boot DevTools restart work, why is it faster than a cold start, and what is a common pitfall?Compare standard JVMHotSwap through a debugger, Spring Boot DevTools restart, enhanced class redefinition (DCEVM-style, as in JetBrains Runtime), and commercial agents such as JRebel. What changes can each handle?A team proposes attaching a Java agent in production to hot-patch method bodies for urgent fixes instead of redeploying. How do you evaluate the proposal?Classpath and jar files
How does the JVM find classes on the classpath? Explain -cp with directories, jars and wildcards, the Class-Path manifest attribute, and what happens when two jars contain the same class.After a dependency upgrade, a service fails at runtime withjava.lang.NoSuchMethodError: 'com.fasterxml.jackson.core.JsonParser com.fasterxml.jackson.core.JsonFactory.createParser(...)'. It compiled fine. How do you find and fix the cause?Your 300 services each pull hundreds of jars, and duplicate or conflicting classes cause recurring incidents. What standards would you put in place for dependency and classpath hygiene?Loading classpath resources
This code works in the IDE but returns null when run from the packaged jar:new File("src/main/resources/templates/mail.html") and getClass().getResource("templates/mail.html"). Explain both problems and the correct way.A library loads plugins from META-INF/services and configuration files from the classpath. In a Spring Boot fat jar and in a Jakarta EE server, some resources are not found or are found twice. Explain how resource lookup differs and how to make the library robust.ClassNotFoundException vs NoClassDefFoundError
What is the difference betweenClassNotFoundException and NoClassDefFoundError? Give a typical cause of each.A Spring Boot 3 application migrated from Boot 2 fails at startup with java.lang.NoClassDefFoundError: javax/servlet/Filter coming from a third-party library. What is going on and what are your options?No question matches these choices.