Java.lang.illegalstateexception Classfinder Did Not Find Any Classes

7 min read

The java.And in short, the message means that a component responsible for scanning your compiled output — the **ClassFinder** — looked inside the directory where your . Think about it: illegalStateException: ClassFinder did not find any classes error is one of those build-time messages that stops a Java project dead in its tracks, usually right when you are trying to run or package a Spring Boot application. So class files are supposed to live and found nothing at all. It looks intimidating, but it is actually one of the more straightforward errors to diagnose once you understand what the build tool is trying to do. Still, lang. No classes, no main class, no application to launch Which is the point..

This article walks through why that happens, how to confirm the root cause in your specific project, and the exact steps to fix it in both Maven and Gradle setups. By the end, you will not only know how to resolve the error but also how to prevent it from creeping back into your workflow.

What the Error Actually Means

When you run a Spring Boot build command such as mvn spring-boot:run, mvn spring-boot:repackage, or gradle bootJar, the plugin needs to locate your application's entry point — the class annotated with @SpringBootApplication that contains the public static void main(String[] args) method. To do that, it uses an internal utility called a ClassFinder, which scans a directory (typically target/classes in Maven or build/classes in Gradle) and returns the list of compiled classes it discovers.

The IllegalStateException is thrown when that scan returns an empty result. The plugin is effectively saying: "I was told to look for classes here, but this location contains zero class files, so I cannot continue." It is not a problem with your Java code, your dependencies, or your Spring configuration. It is a problem with the build output — the compiled artifacts the plugin expects to find are missing, empty, or located somewhere the plugin is not looking.

The Most Common Causes

Understanding the root cause narrows the fix down to a single command in most cases Most people skip this — try not to..

The causes almost always fall into one of a handful of categories. Once you know which one applies to you, the fix is usually a single command.

1. You never compiled the project

This is by far the most frequent culprit. Which means the spring-boot:run and repackage goals do not always trigger a fresh compile on their own, and if you have never run a build, the output directory simply does not exist yet. Think about it: the ClassFinder points at target/classes, finds an empty or missing folder, and gives up. A clean checkout that has never been built is the classic scenario.

2. The output directory was wiped by a clean

Running mvn clean or gradle clean deletes the compiled output. If you clean and then invoke a goal that expects classes to already be present — without a compile step in between — you recreate the empty-directory condition. This is common in CI pipelines where stages are split into separate jobs and the workspace does not carry compiled artifacts between them.

Most guides skip this. Don't And that's really what it comes down to..

3. Your source files are not where the build tool expects them

Maven assumes sources live in src/main/java and Gradle makes the same default assumption. If your .Plus, java files sit in a non-standard location — say src/ directly, or a custom package root — the compiler produces nothing, and the output directory stays empty. The build succeeds quietly because there was nothing to compile, then the plugin fails when it goes looking Which is the point..

4. A misconfigured build file

Occasionally the source or output directory is explicitly overridden in your pom.xml or build.Also, gradle to a path that does not line up with reality. A typo in <sourceDirectory>, a stray sourceSets block, or a build/classes path that has been redirected will all send the ClassFinder to the wrong place.

5. Compilation failed earlier but was ignored

If a previous compile step errored out, no classes were written. Some wrapper scripts or IDE run configurations suppress that earlier failure and proceed straight to the run goal, so the ClassFinder error is really a downstream symptom of a compilation problem you have not seen yet That's the whole idea..

Confirming the Root Cause

Before changing anything, verify what is actually on disk. This takes seconds and tells you which of the causes above you are dealing with.

Check whether the output directory exists and whether it contains anything:

# Maven
ls -la target/classes

# Gradle
ls -la build/classes/java/main

If the directory is missing or empty, you have confirmed the diagnosis: there is nothing for the ClassFinder to find. Next, force a clean compile and watch for errors, because a failed compile is often the real story:

# Maven — verbose so you can see the compile phase
mvn clean compile

# Gradle
gradle clean classes

Read the output of that compile carefully. If it reports BUILD SUCCESS and classes now appear in the directory, you are done. If it reports compilation errors, fix those first — the ClassFinder message will disappear on its own once real classes exist Small thing, real impact. Simple as that..

Fixing It in Maven

The reliable sequence is to compile before you run or package. Rather than calling spring-boot:run in isolation, chain it to a phase that guarantees compilation:

mvn clean spring-boot:run

The spring-boot:run goal binds to the compile phase, so this recompiles your sources and then launches the application. For packaging, the equivalent is:

mvn clean package

The repackage goal runs as part of package and will find your classes as long as the compile phase produced them. If you prefer to be explicit, mvn clean compile spring-boot:run works too and makes the intent obvious But it adds up..

If a clean compile still produces an empty target/classes, inspect your pom.xml for a custom build configuration:


    src/main/java
    target/classes

Confirm these paths match where your source files actually live. A mismatch here is the fix.

Fixing It in Gradle

Gradle's bootRun task depends on the classes task, so a normal invocation compiles first:

gradle clean bootRun

For an executable jar, use bootJar, which also depends on compilation:

gradle clean bootJar

If classes still do not materialize, check your build.gradle for a sourceSets block that redirects the source or output directories:

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java']
        }
        resources {
            srcDirs = ['src/main/resources']
        }
    }
}

Make sure srcDirs points at the real location of your .Here's the thing — java files. A common mistake is a leftover srcDirs = ['src'] that causes the compiler to scan the wrong root and produce nothing.

Preventing the Error from Returning

The underlying lesson is that the run and package goals assume compilation

has already happened. Treat compilation as an explicit, verifiable step rather than an implicit side effect. A few habits keep the problem from recurring:

  • Always run a clean build after switching branches, pulling changes, or editing build files. Stale or partial output directories are the most common trigger.
  • Keep your build tool's output directory in version control ignore rules (.gitignore should list target/ and build/), but never commit compiled classes. Committing artifacts invites drift between source and bytecode.
  • If you use an IDE, make sure its compiler output is aligned with the command-line tool. Running mvn spring-boot:run from a terminal while the IDE writes to a different folder is a classic source of confusion.
  • In CI, invoke clean as part of the pipeline so every build starts from a known state. A pipeline that reuses a workspace across runs can accumulate exactly the kind of empty-or-partial output that produces this error.

When you do hit the message again, resist the urge to add classpath hacks or move files around. The ClassFinder is reporting a symptom, not a cause. The cause is almost always that no compiled classes exist where the tool expects them, and the fix is to make the compile step run and succeed.

Conclusion

The "Unable to find main class" or empty-ClassFinder error in a Spring Boot application is rarely about the finder itself. Still, it is a build-ordering problem: the run or package goal executed against a target/classes or build/classes/java/main directory that was never populated. Because of that, the diagnosis is straightforward — check whether compiled classes exist, then force a clean compile and read the output. If the compile fails, fix the real errors; if it succeeds, the finder problem resolves itself.

The durable fix is to stop treating compilation as something that happens for you. Chain your goals so compilation always precedes execution (mvn clean spring-boot:run, gradle clean bootRun), verify your sourceDirectory/outputDirectory or sourceSets configuration points at the real source tree, and keep clean builds as the default in both your local workflow and your CI pipeline. Once compilation is a deliberate, confirmed step, this error loses its ability to surprise you Took long enough..

Fresh Stories

Out This Morning

People Also Read

Parallel Reading

Thank you for reading about Java.lang.illegalstateexception Classfinder Did Not Find Any Classes. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home