java静态嵌套类访问引发NoClassDefFoundError
我尝试使用由静态嵌套类组成的实用程序类来实现公共功能。这些静态嵌套类正在实现命令样式的接口:
public interface BooleanFunction{
public boolean execute();
}
包含这些实现此接口的公共类的类是:
public class ExBooleans {
public static class isComponentOpen implements BooleanFunction {
private int widgetId;
private int componentId;
public isComponentOpen(int widgetId, int componentId) {
this.widgetId = widgetId;
this.componentId = componentId;
}
@Override
public boolean execute() {
return Widgets.getComponent(this.widgetId, this.componentId) != null;
}
}
这就是所谓的:
ExUtilities.makeCondition(new ExBooleans.isComponentOpen(RANGE_WIDGET_ID, RANGE_COOK_COMPONENT_ID), 1000)
其中makeCondition
接受BooleanFunction
:
public static boolean makeCondition (final BooleanFunction booleanFunction, int timeout){
return Utilities.waitFor(new Condition() {
@Override
public boolean validate() {
return booleanFunction.execute();
}
}, timeout);
}
这一切都是为了为Utilities.waitFor(Condition c, int timeout)
函数提供一个包装器,使代码更清晰、可读性更强
但是,当我调用makeCondition
并传入ExBooleans.isComponentOpen
时,我收到一个运行时错误:
Unhandled exception in thread ~threadnumber~: java.lang.NoClassDefFoundError: api/ExBooleans$isComponentOpen
在包含来自上面的呼叫的线路上:
ExUtilities.makeCondition(new ExBooleans.isComponentOpen(RANGE_WIDGET_ID, RANGE_COOK_COMPONENT_ID), 1000)
如果您能帮助解决此问题,我们将不胜感激
# 1 楼答案
我能够通过将
interface
和makeCondition
方法拉入一个单独的类来解决这个问题,该类包含了这些方法和实用工具实现isComponentOpen
,等等。由于这些方法都嵌套在一个类中,我不再得到错误,将代码分组在一起可能更有意义我仍然不确定错误是从哪里来的