ClassNotFoundException in Java — What It Means and How to Fix It

What Java's ClassNotFoundException means and how to fix it.

Published November 3, 2026

ClassNotFoundException is thrown when the JVM cannot find a class by name at runtime, usually a missing library on the classpath.

What it means and why it happens

Typical causes: missing dependency, wrong classpath, typos in names loaded with Class.forName, or version mismatches.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

Practical examples

Code that triggers it

Class.forName("com.mysql.cj.jdbc.Driver");

The fix

// add the dependency
// <dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency>
Class.forName("com.mysql.cj.jdbc.Driver");

Inspect the classpath

java -cp "app.jar:lib/*" Main
mvn dependency:tree

Common mistakes

  • Forgetting runtime dependencies.
  • Case or package typos.
  • Mixed library versions.

Key takeaways

  • ClassNotFoundException: ClassNotFoundException is thrown when the JVM cannot find a class by name at runtime, usually a missing library on the classpath.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

ClassNotFound vs NoClassDefFound?

The first is a checked exception when loading by name; the second is an Error when a class existed at compile time but not at runtime.

How do I find which line caused ClassNotFoundException?

The error message and stack trace show the file and line; start there and work backwards.

More Java articles