简体   繁体   English

Java错误:找到了接口......但是预期了类

[英]Java error: Found interface … but class was expected

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). 当我将一个实现绑定到接口时,问题就消失了(似乎@ImplementedBy注释还不够)。

I was more interested in the mechanics through which Guice managed to actually instantiate an interface. 我对Guice设法实际实例化接口的机制更感兴趣。

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. 编译应用程序时,类(在您的问题中名为SomeInterface )作为类存在。

When your application is running at compile time, SomeInterface exists as an interface (instead of a class.) 当您的应用程序在编译时运行时, SomeInterface作为接口(而不是类)存在。

This causes an IncompatibleClassChangeError to be thrown at runtime. 这会导致在运行时抛出IncompatibleClassChangeError

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. 如果编译时类路径上的jar文件版本与运行时类路径上的版本不同,则这是常见的情况。

很可能代码是针对库中的类编译的,然后将其更改为您运行的版本中的接口。

I had the same problem. 我有同样的问题。 I use two jar libraries in my application. 我在我的应用程序中使用了两个jar库。 One library is built upon the other. 一个库建立在另一个库上。

The library A defines top classes and interfaces. 库A定义了顶级类和接口。 Library B requires library A. 图书馆B需要图书馆A.

This is pseudocode of some code used in library B: 这是库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. 显然,库A比库B更新,并且TheInterface不再包含someMethod ,但是TheClass仍然可以。 The only way to fix this is to get the source for either jar and change these things by hand (if at all possible). 解决这个问题的唯一方法是获取任一jar的源代码并手动更改这些内容(如果可能的话)。

This happened to me when i was running a maven build . 当我运行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 . 从我可以收集的内容(以及Jared的答案 )的原因是 - 在我的有效pom.xml指定了相同的第三方jar的两个版本。 One version was coming in as a transitive dependency and the other was specified by me in my local pom.xml . 一个版本作为传递依赖进入,另一个版本由我在本地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. 我删除了本地pom.xml指定的版本,但它确实有效。

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. 但他们可以自由地这样做。

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. 编辑2:看起来Jared有正确的答案。 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. 无论如何,尝试扩展接口实际上会在编译时给出“ 没有预期的接口 ”消息,而不是“ 找到的接口但是预期的类 ”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM