How to Fix "Found interface but class was expected" Error in Java


How can we fix the Found interface RandomInterface but class was expected error in Java?

Suppose we have the following interface:

interface RandomInterface {}

This IncompatibleClassChangeError error can be thrown at runtime when our runtime classpath is different than our compile-time classpath.

A classpath refers to the set of all the classes and jars required to run our application.

When our application compiles, RandomInterface may exist as a class.

When our application runs at compile time, RandomInterface might exist as an interface, causing this error.

3rd party libraries and jars

In the case of 3rd party jars, this is generally a backwards-compatibility issue. We might have different versions of the jar in the runtime and compile-time classpath.

We would simply need to verify that the versions we compile and execute on are the same.

Also, we should verify that all libraries requiring the same versions to be compatible are actually the same in our pom.xml.

Multi-module projects

This error is also common in multi-module projects. A full rebuild of the project generally solves this issue.

The code was probably compiled against a class in another module, but it was changed to an interface in the version we’re currently running against.