Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

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?

The class is in the jar; what failed was its initialisation. The first time something used Config, its static initialiser threw an exception, which the JVM reported once as ExceptionInInitializerError and then marked the class as permanently failed. Every later use throws NoClassDefFoundError: Could not initialize class com.acme.Config, so we search the logs for the first ExceptionInInitializerError and its cause, usually a missing property or a failed file read in a static block.

Why this question is asked

This error sends many developers in the wrong direction. The message says NoClassDefFoundError, so they check the jar, rebuild, compare class paths, and find the class sitting exactly where it should be. Interviewers ask it because the answer shows whether we know the difference between finding a class and initialising it, and whether we know where the real evidence lives in the logs. In this answer, we will recap class initialisation, reproduce the error, see why the later errors hide the cause, go through how to find the first failure, and compare it with ClassNotFoundException.

1A Quick Recap of Class Initialisation

Before a class can be used, the JVM loads its bytes, links it, and then initialises it by running its static initialiser: all static field initialisers and static blocks, in the order they appear. Initialisation happens once, the first time the class is actively used, for example when we call a static method or create an instance.

The rule that matters here is what happens when that code throws. If a static initialiser throws an exception, the JVM wraps it in an ExceptionInInitializerError (unless it is already an Error) and throws that to the code that triggered initialisation. It also records that the class is in an erroneous state. The JVM never retries the initialisation of that class in that class loader.

2Reproducing the Error

Here is a small Config class that reads a required setting in a static block, and two calls to it.

Java
public class Config {
static final String DB_URL;
static {
String v = System.getenv("DB_URL");
if (v == null) throw new IllegalStateException("DB_URL is not set");
DB_URL = v;
}
public static String dbUrl() { return DB_URL; }
}
try { Config.dbUrl(); } catch (Throwable t) { System.out.println(t); }
// java.lang.ExceptionInInitializerError
// Caused by: java.lang.IllegalStateException: DB_URL is not set
try { Config.dbUrl(); } catch (Throwable t) { System.out.println(t); }
// java.lang.NoClassDefFoundError: Could not initialize class Config
Step throughWhat the JVM does on each use of Config
  1. Config already initialised?
    YesUse it normallyNoCheck its state
  2. Marked as failed earlier?
    YesThrow NoClassDefFoundError: Could not initialize classNoRun the static initialiser
  3. Static initialiser threw?
    YesMark failed, throw ExceptionInInitializerErrorNoMark initialised, continue

3 steps in order. Play them, or step through them one at a time.

Only the very first use shows the real cause.

Notice that the two calls are identical, but the errors are different. The first call actually runs the static block, and we see the real cause, IllegalStateException with a clear message. The second call never runs the static block again; the JVM sees the class is marked as failed and throws NoClassDefFoundError immediately.

3Why the Real Cause Is Easy to Miss

In a service, the first use of Config often happens during start-up or on the first request, and the ExceptionInInitializerError may be caught and logged by a framework, a thread pool or a scheduler, sometimes at a level we do not read. After that, every request that touches Config fails with NoClassDefFoundError, so the logs fill with thousands of copies of the second error and the single interesting stack trace scrolls away, or rotates out of the log file entirely.

Newer JDKs help here: the NoClassDefFoundError thrown for a class whose initialisation failed carries a cause that describes the original ExceptionInInitializerError, including the exception type and the thread that hit it (check your version; this was added in recent releases, around JDK 18). On older JDKs the later errors contain no hint at all.

Java
java.lang.NoClassDefFoundError: Could not initialize class com.acme.Config
at com.acme.OrderService.place(OrderService.java:42)
Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.IllegalStateException:
DB_URL is not set [in thread "main"]

4Where to Look and How to Fix It

The investigation has three steps. First, search all logs since the process started for ExceptionInInitializerError, or for the class name together with Caused by. In a centralised log system, sort by time and take the earliest occurrence for this instance. Second, read its cause. Typical causes are a missing environment variable or system property, a configuration file read from a path that does not exist in production, a resource loaded with a wrong name that returns null, a parse error in a static Pattern or date format, or a dependency class that is itself missing. Third, if the logs are gone, restart one instance with the logs captured from the start; the failure will happen again on the first use.

The lasting fix is to stop doing fallible work in static initialisers. Configuration should be read by explicit start-up code, such as a Spring @ConfigurationProperties class or a bean constructor, where a failure stops the application with a clear message and can be retried after fixing it. A static block can only fail once, and then it poisons the class for the life of the class loader, which in practice means the life of the JVM.

5How This Differs From ClassNotFoundException

The two are often confused, but they mean different things.

Three errors that look alike
ErrorWhat it meansWhere the cause is
ClassNotFoundException: com.acme.Configloading by name found no bytesclass path or the name used
NoClassDefFoundError: com/acme/Configcompiled against, missing at run timemissing jar or wrong scope
NoClassDefFoundError: Could not initialize classfound, but static init failed earlierfirst ExceptionInInitializerError
ExceptionInInitializerErrorstatic init threw just nowits Caused by line

ClassNotFoundException means the loader never found the class bytes at all, typically from Class.forName with a wrong or missing class name. NoClassDefFoundError is broader: either a class the code was compiled against could not be found at run time, which shows the class name in slash form, such as com/acme/Config, or, as here, the class was found but its initialisation failed. The words Could not initialize class are the tell: they mean the bytes were found, so checking the jar is wasted effort.

6Follow-up questions

Why is heavy work in static initializers a risk for services?

Because a static initialiser runs at an unpredictable moment, the first use, often inside a request thread, and if it fails it can never be retried: the class stays broken until the JVM restarts. It also runs while holding the class's initialisation lock, so slow work such as network calls blocks every other thread that touches the class, and dependencies between static initialisers can deadlock. Explicit start-up code fails fast, visibly and retryably instead.

Can the same failure happen without a static block?

Yes. Any static field initialiser is part of the static initialiser, so static final Pattern P = Pattern.compile(badRegex) or static final Client C = new Client(readConfig()) fails the same way. An enum whose constant constructors throw also fails to initialise, and every later use of the enum gives the same NoClassDefFoundError.

Does restarting fix it?

Only if the cause was temporary, such as a file that was not mounted yet when the class was first used. If the cause is a missing setting, the class fails again on the first use after restart. Either way, a restart is a workaround; the fix is the cause in the first error.

7Conclusion

In this answer, we saw that NoClassDefFoundError: Could not initialize class means the class was found but its static initialiser failed on an earlier use. That first failure was reported as ExceptionInInitializerError with the real cause, the JVM marked the class as failed, and every later use throws the shorter error without running the initialiser again. We find the cause by searching for the first ExceptionInInitializerError, newer JDKs attach it as the cause of the later error, and the lasting fix is to move fallible work out of static initialisers.

What to say in the interviewAbout 90 seconds aloud

Open with

The key words are Could not initialize class. They mean the JVM found the class bytes; what failed is its initialisation. The first time something used Config, its static initialiser, the static blocks and static field initialisers, threw an exception.

Then, if they want more

The JVM wrapped that in an ExceptionInInitializerError, threw it to that first caller, and marked the class as erroneous. It never retries initialisation, so every later use of Config throws NoClassDefFoundError: Could not initialize class, without running the static code again and, on older JDKs, without any hint of the cause.

So I search the logs from the start of the process for the first ExceptionInInitializerError, or for Caused by near the Config class name, and read its cause.

Usually it is a missing environment variable or property, a file path that exists on a laptop but not in production, a resource that returned null, or a dependency missing at run time. On newer JDKs the later NoClassDefFoundError carries the original failure as its cause, which helps, but you should check your version.

If the logs have rotated, I restart one instance and capture the first failure. This is different from ClassNotFoundException, which means the bytes were never found. The long-term fix is to move fallible work like reading configuration out of static initialisers into explicit start-up code.

About this answer

Last updated
12 November 2026
Versions it names
Java 18
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Logs show java.lang.NoClassDefFoundError: Could not initialize class com.acme.Config...": https://scrivio-interview-library.netlify.app/java/class-loading/7bb81cac70/