IllegalAccessError:class<类名>;无法访问其超级接口 类名>
我有实现 IAssembly 的 Assembly 类。
时出现以下错误
Caused by: java.lang.IllegalAccessError: class <Assembly > cannot access its superinterface <IAssembly>
at java.lang.ClassLoader.defineClass1(Native Method)
启动应用程序程序集代码
class package.Assembly implements IAssembly {
}
IAssembly
interface IAssembly { //note -this is not public, so uses default protected
}
,IAssembly 存在于两个不同的 jar 中。两个 jar 都由不同的类加载器加载。 Assembly 类在子类加载器中加载,IAssembly 是父类。类加载器使用链接。
在正常情况下,这是有效的。当我使用 cobertura 检测 jar 后运行应用程序时,会发生错误。无需仪器,一切正常。 cobertura 仪器会导致这样的错误吗?或者这是一个等待检测的错误,但使用 cobertura 后,错误很快就会暴露出来。
通过将接口设置为“公共”,错误就会消失。
I have class Assembly implementing IAssembly.
I see following error when starting the application
Caused by: java.lang.IllegalAccessError: class <Assembly > cannot access its superinterface <IAssembly>
at java.lang.ClassLoader.defineClass1(Native Method)
Assembly code
class package.Assembly implements IAssembly {
}
IAssembly
interface IAssembly { //note -this is not public, so uses default protected
}
Assembly and IAssembly exists in two different jars. Both jars loaded by different classloaders. The Assembly class is loaded in child classloader, IAssembly is parent. Class loaders are using chaining.
In normal cases, this works. The error occurs when I run my application after instrumenting jars using cobertura. With out instrumentation all works fine. Could cobertura instrumentation cause such error? Or This is an error anyway waiting to be detected, but with cobertura the error is quickly exposed.
By making the interface 'public' the error goes away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,即使加载器是链接的,包保护也会因检测和多个类加载器而失败。此 javadoc 位于 java.lang.instrument.Instrumentation 与您的场景没有直接关系,但它确实描述了类似的场景:
也许检查哪个加载器正在查找您的检测类,并查看是否有办法让
Assembly
和IAssembly
从同一个类加载器加载。It looks to me like package-protection fails with instrumentation and multiple classloaders, even if the loaders are chained. This javadoc on java.lang.instrument.Instrumentation isn't directly related to your scenario, but it does describe a similar scenario:
Maybe check which loader is finding your instrumented classes, and see if there is a way to get both
Assembly
andIAssembly
to load from that same classloader.我认为您的问题可能是您没有使用 IAssembly 的兼容版本。因此,即使它位于您的类路径中,接口及其实现也不匹配。
如果是类加载器问题,您将收到 NoClassDefFoundError。
I think your problem may be that you are not using a compatible version of IAssembly. Thus even though it is in your classpath, the interface and its implementation do not match.
If it was a classloader issue, you would get a NoClassDefFoundError.
我将添加导致此错误消息的另外两个原因。
protected
更改为public 或
private
到protected
或 .... 我知道这不是原因,因为提问者知道看到他的评论。I'll just add two more reasons for this error message
protected
topublic
orprivate
toprotected
or .... I know it isn't the reason here as the questioner was aware see his comment.