1

我正在开发一个使用 Apache Felix OSGi 框架的 JavaFX 应用程序。我的问题是,当尝试注册自定义事件时,会引发下面显示的异常。

org.apache.karaf.shell.support.MultiException: Error executing command on bundles:
    ...
Caused by: java.lang.NoClassDefFoundError: Could not initialize class swt6.componentmanager.gui.event.RemoveSensorEvent
    at swt6.componentmanager.gui.ComponentManagerGui.createSensorView(ComponentManagerGui.java:170) ~[?:?]
    at swt6.componentmanager.gui.ComponentManagerGui.createSensorActuatorContainer(ComponentManagerGui.java:159) ~[?:?]
    at swt6.componentmanager.gui.ComponentManagerGui.createRootContainer(ComponentManagerGui.java:91) ~[?:?]
    at swt6.componentmanager.gui.ComponentManagerGui.lambda$start$0(ComponentManagerGui.java:62) ~[?:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[?:?]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) ~[?:?]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:?]
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) ~[?:?]
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) ~[?:?]
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) ~[?:?]
    at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277) ~[?:?]
    ... 1 more

注册自定义事件的代码也如下所示。

private Node createSensorView() {
    var container = new VBox();
    container.getChildren().add(JavaFXUtil.createHeading("Sensors"));
    var sensorTable = new SensorsTable(this.sensors);
    sensorTable.addEventHandler(
            RemoveSensorEvent.REMOVE_SENSOR_EVENT_TYPE,
            this::onRemoveSensor
    );
    container.getChildren().add(sensorTable);
    return container;
}

该类的代码RemoveSensorEvent也如下所示。

package swt6.componentmanager.gui.event;

import javafx.event.Event;
import javafx.event.EventType;
import swt6.sensor.Sensor;

public class RemoveSensorEvent extends Event {
    public static final EventType<RemoveSensorEvent> REMOVE_SENSOR_EVENT_TYPE =
            new EventType<>(Event.ANY, "REMOVE_SENSOR");

    private final Sensor sensor;

    public RemoveSensorEvent(Sensor sensor) {
        super(REMOVE_SENSOR_EVENT_TYPE);

        this.sensor = sensor;
    }

    public Sensor getSensor() {
        return this.sensor;
    }
}

包含 JavaFX 代码的包具有以下包布局;所有这些包都位于同一个包中:

swt6.componentmanager.gui
    ComponentManagerGui.java
    swt6.componentmanager.gui.event
        RemoveSensorEvent.java
    swt6.componentmanager.gui.component
    swt6.componentmanager.gui.viewmodel

作为RemoveSensorEvent包的一部分swt6.componentmanager.gui.event,它与调用它的代码位于同一个包中(swt6.componentmanager.gui.ComponentManagerGui.java),加载类定义应该没有任何问题;例如,位于swt6.componentmanager.gui.component并且swt6.componentmanager.gui.viewmodel可以毫无问题地访问的类swt6.componentmanager.gui。这与尝试RemoveSensorEvent使用反射访问的 JavaFX 有关吗?

4

0 回答 0