Java错误:找到接口......但是预期了类 [英] Java error: Found interface ... but class was expected

查看:91
本文介绍了Java错误:找到接口......但是预期了类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从代码中收到一个奇怪的运行时错误:

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?

更新:(响应一些答案)我正在编译并运行同一组库,但我使用 Guice 为此注入提供商特殊界面。

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.

当我将一个实现绑定到接口时,问题就消失了(好像@ImplementedBy注释还不够)。

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

我对Guice设法实际实例化接口的机制更感兴趣。

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

推荐答案

当你的运行时类路径与编译时类路径不同。

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

编译应用程序时,一个类(名为 SomeInterface in你的问题)作为一个类存在。

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.)

这会导致在运行时抛出 IncompatibleClassChangeError

This causes an IncompatibleClassChangeError to be thrown at runtime.

如果编译时类路径上的jar文件版本与运行时类路径上的版本不同,则这是常见的。

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.

这篇关于Java错误:找到接口......但是预期了类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆