我正在尝试使用由静态嵌套类组成的实用程序类来实现常见功能。这些静态嵌套类正在实现命令样式接口(interface):
public interface BooleanFunction{
public boolean execute();
}
持有这些实现此接口(interface)的公共(public)类的类是:
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)
任何解决此问题的帮助将不胜感激!
最佳答案
我能够通过将 interface
和 makeCondition
方法拉入一个单独的类来解决该问题,该类同时容纳这些方法和实用程序实现 isComponentOpen
等。通过将这些全部嵌套在一个类中,我不再收到错误,并且代码组合在一起可能更有意义。
尽管如此,我仍然不确定错误是从哪里来的。
关于java - 静态嵌套类访问抛出 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377172/