Java 错误:找到接口...但需要类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/591411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java error: Found interface ... but class was expected
提问by levik
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 amusing Guiceto inject a Provider for this particular Interface.
更新:(回应一些答案)我正在编译和运行同一组库,但我正在使用Guice为这个特定接口注入一个提供者。
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 设法实际实例化接口的机制更感兴趣。
采纳答案by Jared
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.
这会导致 anIncompatibleClassChangeError
在运行时被抛出。
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 文件版本与运行时类路径上的 jar 文件版本不同,那么这种情况很常见。
回答by Michael Myers
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 runtimeerror and not a compile-timeerror? 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.
编辑 2:看起来 Jared 有正确的答案。无论如何,尝试扩展接口实际上会在编译时给出“这里没有预期的接口”消息,而不是“找到接口但需要类”错误。
回答by starblue
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.
很可能代码是针对库中的类编译的,然后将其更改为您运行的版本中的接口。
回答by Mark Jeronimus
I had the same problem. I use two jar libraries in my application. One library is built upon the other.
我有同样的问题。我在我的应用程序中使用了两个 jar 库。一个库建立在另一个之上。
The library A defines top classes and interfaces. Library B requires library A.
库 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. The only way to fix this is to get the source for either jar and change these things by hand (if at all possible).
显然库 A 比库 B 新并且TheInterface
不再包含someMethod
,但TheClass
仍然包含。解决此问题的唯一方法是获取任一 jar 的源代码并手动更改这些内容(如果可能的话)。
回答by Pradeep Anchan
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
. One version was coming in as a transitive dependencyand the other was specified by me in my local pom.xml
.
从我可以收集到的(以及从 Jared 的回答),原因是 - 在我的有效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.
当然,第 3 方破坏了他们版本之间的向后兼容性,并将类更改为接口,反之亦然。但他们可以自由地这样做。