Fix java.lang.ClassNotFoundException: com.example.MyClass - Troubleshooting Guide

intermediate Java2026-03-16| Linux, macOS, Windows with Java Development Kit (JDK), Apache Maven, Gradle, IntelliJ IDEA, Eclipse, Apache Tomcat, JBoss/WildFly, Spring Boot, etc.

Error Message

java.lang.ClassNotFoundException: com.example.MyClass
#java#classpath#classloader

java.lang.ClassNotFoundException: com.example.MyClass

The java.lang.ClassNotFoundException is a common runtime error. It occurs when the Java Virtual Machine (JVM) attempts to load a class using its string name but can't locate its definition. This usually means the class file (.class) is missing from the Java classpath during execution, even if it was present during compilation. When this exception appears, it's a clear sign to investigate where your JVM is searching for code.

What’s Happening Under the Hood?

Essentially, your application (or a library it relies on) is trying to use com.example.MyClass. The JVM's class loader diligently searches the defined classpath for the corresponding MyClass.class file. If, after checking all specified locations, it fails to find the file, it throws this exception. Think of it like trying to grab a specific tool from your toolbox, only to discover it's not there.

Step-by-Step Fix

1. Verify Class Name and Package Path

First, rule out the simplest mistake: a typo. Double-check that com.example.MyClass is the exact fully qualified name of the class you need. Remember, Java is case-sensitive; myclass is not the same as MyClass.

Next, confirm that the .class file for MyClass actually exists. It should be located in a directory structure that precisely mirrors its package declaration. For instance, com.example.MyClass implies you'll find MyClass.class inside a com/example/ directory, relative to a classpath entry.

2. Check Your Java Classpath

The classpath is essentially a map, guiding the JVM to where it should look for user-defined classes and packages. An incorrect or incomplete classpath is by far the most frequent culprit behind a ClassNotFoundException.

a. Standalone Java Applications When running a JAR file, use the -cp or -classpath argument to specify all required JARs and directories.

# If MyClass is in myapp.jar:
java -cp myapp.jar com.example.MainClass

# If MyClass is in an external library (e.g., library.jar) and myapp.jar depends on it:
java -cp myapp.jar:library.jar com.example.MainClass  # Linux/macOS
java -cp myapp.jar;library.jar com.example.MainClass  # Windows

# To include all JARs in a 'lib' directory (Java 8 and earlier):
java -cp "myapp.jar:lib/*" com.example.MainClass # Linux/macOS
java -cp "myapp.jar;lib/*" com.example.MainClass # Windows

# Or, set the CLASSPATH environment variable (less recommended for production):
export CLASSPATH=myapp.jar:library.jar # Linux/macOS
set CLASSPATH=myapp.jar;library.jar # Windows
java com.example.MainClass

# To inspect your current CLASSPATH environment variable:
echo $CLASSPATH # Linux/macOS
echo %CLASSPATH% # Windows

b. Verifying JAR Content It's crucial to confirm that the JAR file you suspect contains com.example.MyClass actually does. You can easily inspect a JAR's contents using the jar command-line utility:

jar tvf my_dependency.jar | grep com/example/MyClass.class

If this command doesn't return anything, the class is not in that JAR.

3. Review Build Tool Dependencies (Maven/Gradle)

If you're working with Maven or Gradle, the issue often stems from how dependencies are declared or managed. These build tools automatically handle your classpath. Therefore, an error here usually indicates that a required library isn't being correctly included.

a. Maven Examine your pom.xml for the dependency that provides com.example.MyClass. Verify its declaration and ensure the scope is appropriate. For instance, avoid scopes like <scope>provided</scope> or <scope>test</scope> if the class is needed at runtime.

<dependency>
    <groupId>group.id</groupId>
    <artifactId>artifact-id</artifactId>
    <version>1.0.0</version>
    <!-- Ensure scope is NOT 'provided' or 'test' if needed at runtime -->
</dependency>

Run a dependency tree analysis to see what's actually on your runtime classpath:

mvn dependency:tree

After any changes, always clean and rebuild:

mvn clean install

b. Gradle Similarly, inspect your build.gradle file. Confirm that the dependency is correctly added to a runtime configuration, such as implementation or runtimeOnly.

dependencies {
    implementation 'group.id:artifact-id:1.0.0'
    // Ensure it's not 'testImplementation' or 'compileOnly' if needed at runtime
}

Check the resolved dependencies:

gradle dependencies

Then clean and rebuild:

gradle clean build

4. IDE Configuration (IntelliJ IDEA, Eclipse)

If the error appears when running your application directly from an IDE:

- 
    **IntelliJ IDEA:** Navigate to 'File > Project Structure > Modules > Dependencies'. Confirm that all required JARs and modules are listed and have the correct scope. Then, try 'Build > Rebuild Project'.


- 
    **Eclipse:** Right-click your project, then select 'Build Path > Configure Build Path'. Verify that the 'Libraries' and 'Projects' tabs include all necessary components. Also, ensure 'Order and Export' settings are correct. Finally, try 'Project > Clean...'.

Often, a simple refresh of Maven/Gradle dependencies within the IDE, or even re-importing the entire project, can resolve problems where the IDE's internal classpath has become desynchronized.

5. Application Server Deployment (Tomcat, JBoss, WebLogic)

For web applications packaged as WARs or EARs, the application server's classloader is responsible for managing the classpath.

- 
    **WAR files:** Make sure all required third-party JARs reside in your WAR file's `WEB-INF/lib` directory. Your own compiled classes should be in `WEB-INF/classes`.


- 
    **EAR files:** Dependencies can exist at the application level (within the EAR's `lib` directory) or at the module level. It's crucial to understand your specific application server's classloader delegation model.

After carefully verifying the package structure and contents, redeploying the application is frequently required to apply changes.

6. Version Conflicts

This error can also occur if com.example.MyClass belongs to a library present in multiple versions on your classpath. In such cases, one version might be loaded, but another part of your code expects a class that only exists in a different version. While build tools like Maven and Gradle offer mechanisms to handle dependency conflicts, resolving them can sometimes be challenging. Utilize mvn dependency:tree or gradle dependencies to pinpoint any conflicting versions.

Verify the Fix

Once you've applied a potential fix, verifying your changes is straightforward:

- **Rerun Your Application:** Start your Java application again or redeploy your web application.
- **Monitor Logs:** Carefully check your console or application server logs for any new exceptions or further instances of `ClassNotFoundException`. If the error has vanished and your application starts as expected, congratulations – you've likely solved the problem!

However, if the error persists, meticulously revisit each step, paying extremely close attention to the exact paths and configurations. Occasionally, the target class itself is found, but then another class that MyClass depends on goes missing, leading to a similar follow-up error. Always be prepared for a cascade of dependency issues.

Tips to Prevent Future ClassNotFoundException Errors

- **Consistent Build Management:** Always leverage a build tool like Maven or Gradle for robust dependency management. Resist the urge to manually copy JARs; this should only be a last resort.
- **Understand Scopes:** Gain a solid understanding of dependency scopes (e.g., `compile`, `runtime`, `provided`, `test`) in Maven/Gradle. This ensures libraries are available precisely when needed.
- **Synchronize Runtime Environments:** Ensure your runtime environment (e.g., production, staging) perfectly mirrors your development environment's classpath setup. This means having the exact same required JARs in both.
- **Simplify Classpath:** Maintain a clean and concise classpath. Overly long or wildcard-heavy entries can obscure subtle issues and should be avoided.
- **Use Fat JARs for Standalone Apps:** For standalone applications, consider creating a "fat JAR" (also known as an "uber JAR") that bundles all necessary dependencies. Tools such as the Maven Shade Plugin or Gradle's `shadowJar` plugin are excellent for this purpose.

Related Error Notes