All posts

Fix NotFoundError in Java

Resolve ClassNotFoundException, NoClassDefFoundError, and FileNotFoundException in Java applications with systematic debugging steps.

Java NotFoundError Variants

Java has several "not found" errors, each with different root causes. Here's how to distinguish and fix them.

ClassNotFoundException

Thrown when Class.forName() or ClassLoader.loadClass() can't find a class at runtime:

// Error: ClassNotFoundException: com.mysql.cj.jdbc.Driver
// Fix: add the dependency
// Maven:
// <dependency>
//   <groupId>com.mysql</groupId>
//   <artifactId>mysql-connector-j</artifactId>
//   <version>8.2.0</version>
// </dependency>

// Then the driver auto-registers, no need for:
// Class.forName("com.mysql.cj.jdbc.Driver");

NoClassDefFoundError

Different from ClassNotFoundException — the class existed at compile time but is missing at runtime:

# Check if the JAR is in the classpath
java -cp "lib/*:app.jar" com.myapp.Main

# Or use -verbose:class to see where classes load from
java -verbose:class -jar app.jar 2>&1 | grep MyClass

FileNotFoundException

// BAD — path is relative to working directory, not the JAR
new FileInputStream("config.properties");

// GOOD — load from classpath
InputStream is = getClass().getResourceAsStream("/config.properties");
if (is == null) {
    throw new FileNotFoundException("config.properties not found in classpath");
}

Spring Boot Resource Loading

@Value("classpath:templates/email.html")
private Resource emailTemplate;

// Or
Resource resource = new ClassPathResource("data/seed.json");
try (InputStream is = resource.getInputStream()) {
    // Process the file
}

Bugsly captures all three error types with the class name, classpath information, and the calling code location — everything needed to diagnose the missing dependency.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free