Java error: Found interface … but class was expected

后端 未结 5 2064
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 00:55

I am getting a strange runtime error from my code:

\"Found interface [SomeInterface] but class was expected\"

How can this happen? How can an i

相关标签:
5条回答
  • 2021-02-05 01:23

    It sounds like you did

    class MyClass extends SomeInterface
    

    when it should actually be

    class MyClass implements SomeInterface
    

    Am I right?

    EDIT: Oh, you say it's a runtime error and not a compile-time error? Let me look around a bit...

    EDIT 2: It looks like Jared has the correct answer. Anyway, trying to extend an interface would actually give a "no interface expected here" message at compile time, not a "found interface but class was expected" error.

    0 讨论(0)
  • 2021-02-05 01:29

    I had the same problem. I use two jar libraries in my application. One library is built upon the other.

    The library A defines top classes and interfaces. Library B requires library A.

    This is pseudocode of some code used in library B:

    TheInterface instance = new TheClass();
    instance.someMethod();
    

    Apparently library A is newer than library B and TheInterface doesn't contain someMethod anymore, but TheClass still does. The only way to fix this is to get the source for either jar and change these things by hand (if at all possible).

    0 讨论(0)
  • 2021-02-05 01:34

    This happened to me when i was running a maven build.

    From what i could gather (as well as from Jared's answer) as the reason was that - there were two versions of the same 3rd party jar specified in my effective pom.xml. One version was coming in as a transitive dependency and the other was specified by me in my local pom.xml.

    So at compile time, it was referring to the old version and at runtime it was referring to the new version.

    I removed the version specified in my local pom.xml and it worked.

    Of course, the 3rd party had broken backward-compatibility between their versions and changed a class to an interface or vice-versa. But they are free to do so.

    0 讨论(0)
  • 2021-02-05 01:40

    This happens when your runtime classpath is different than your compile time classpath.

    When your application was compiled, a class (named SomeInterface in your question) existed as a class.

    When your application is running at compile time, SomeInterface exists as an interface (instead of a class.)

    This causes an IncompatibleClassChangeError to be thrown at runtime.

    This is a common occurence if you had a different version of a jar file on the compile time classpath than on the runtime classpath.

    0 讨论(0)
  • 2021-02-05 01:46

    Most likely the code was compiled against a class in a library, which was then changed to an interface in the version you run against.

    0 讨论(0)
提交回复
热议问题