tags:

views:

978

answers:

3

I am getting a strange runtime error from my code:

"Found interface [SomeInterface] but class was expected"

How can this happen? How can an interface get instantiated?

Update: (In response to some answers) I am compiling and running against the same set of libraries, but I am using Guice to inject a Provider for this particular Interface.

The problem went away when I bound an implementation to the interface (seems like the @ImplementedBy annotation was not enough).

I was more interested in the mechanics through which Guice managed to actually instantiate an interface.

+1  A: 

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.

Michael Myers
well maybe the code was compiled against a class, but the .class present is another version which has become an interface
chburd
@chburd: Yes, it looks like I was close but didn't quite get it. :)
Michael Myers
Nice effort though
Bill K
+14  A: 

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.

Jared
Nice catch, wildly obscure error.
Steve B.
The stack trace was purely in my own code (and would therefore be useless to anyone else). This is what I found weird. It almost looked like Guice was succeeding in creating an instance of an Interface.
levik
+4  A: 

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.

starblue