Scrivio Interview library

Class loading

28 questions in 10 subtopics.

Class loader hierarchy

ConceptFoundations6 min read
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 does String.class.getClassLoader() return?
DebuggingFoundations7 min read
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?
DebuggingIntermediate7 min read
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?
LeadershipAdvanced7 min read
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?
LeadershipPrincipal7 min read
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

ConceptFoundations7 min read
Explain parent-first delegation in ClassLoader.loadClass. Why can an application not define its own java.lang.String and have it used?
InternalsIntermediate7 min read
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

CodingIntermediate7 min read
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 a PluginApi interface.
DesignAdvanced8 min read
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.
LeadershipPrincipal7 min read
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

CodingFoundations6 min read
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);
}
}
DebuggingFoundations6 min read
Logs show 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?
DebuggingIntermediate7 min read
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.
LeadershipPrincipal8 min read
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

ConceptFoundations6 min read
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?
InternalsIntermediate7 min read
When can the JVM unload a class, and why are classes loaded by the application class loader effectively never unloaded?

Metaspace leaks

ConceptFoundations7 min read
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.
DebuggingIntermediate6 min read
After about 30 redeploys of a WAR in a long-running Tomcat without a restart, the JVM fails with OutOfMemoryError: Metaspace. Describe how you find what is holding the old web application class loaders.

Hot reloading

ConceptFoundations7 min read
How does Spring Boot DevTools restart work, why is it faster than a cold start, and what is a common pitfall?
ConceptIntermediate7 min read
Compare standard JVM HotSwap 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?
ScenarioAdvanced6 min read
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

ConceptFoundations7 min read
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.
DebuggingIntermediate6 min read
After a dependency upgrade, a service fails at runtime with java.lang.NoSuchMethodError: 'com.fasterxml.jackson.core.JsonParser com.fasterxml.jackson.core.JsonFactory.createParser(...)'. It compiled fine. How do you find and fix the cause?
LeadershipAdvanced6 min read
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

DebuggingFoundations6 min read
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.
ScenarioIntermediate7 min read
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

ConceptFoundations5 min read
What is the difference between ClassNotFoundException and NoClassDefFoundError? Give a typical cause of each.
DebuggingIntermediate6 min read
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?