Mvvmfx: initialize-method causes NoClassDefFoundError: javax/annotation/PostConstruct

Mvvmfx: initialize-method causes NoClassDefFoundError: javax/annotation/PostConstruct

我正在尝试使用 JavaFx 实现 mvvm 模式。为此,我使用了 mvvmfx 框架。

要使用模型中的数据,我尝试使用 NotificationCenter。当在我的 viewModel 中插入一个 initialize-method 来订阅消息时,我得到一个异常:NoClassDefFoundError: javax/annotation/PostConstruct.

如果有人能告诉我我做错了什么,我将不胜感激。

这是我的视图模型:

@Singleton
public class BasementVM implements ViewModel {

    @Inject
    private NotificationCenter notificationCenter;

    private NotificationObserver observer; // 1

    private StringProperty basementViewKesselTempLbl = new SimpleStringProperty("27");

    public StringProperty KesselTemperatur(){
        return basementViewKesselTempLbl;
    }

    public String getKesselTemperatur(){
        return basementViewKesselTempLbl.get();
    }

    public void setKesselTemperatur(String message){
        basementViewKesselTempLbl.set(message);
    }

    public void initialize() {
        // 2
        observer = (key, payload) -> {
            setKesselTemperatur(payload.toString());
        };

        notificationCenter.subscribe("test", new WeakNotificationObserver(observer)); // 3

    }

}

这是我启动应用程序的地方

public class Main extends MvvmfxGuiceApplication{

    private static Logger logger = Logger.getLogger(Main.class);

    @Override
    public void startMvvmfx(Stage stage) throws Exception {
        stage.setTitle("Hello World Application");

        ViewTuple<DashBoardView, DashBoardVM> viewTuple = FluentViewLoader.fxmlView(DashBoardView.class).load();
        Parent root = viewTuple.getView();
        stage.setScene(new Scene(root));
        stage.show();
    }

public static void main(String[] args) {
        Application.launch(args);
    }
}

这里是 Stacktrace:

java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:945)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NoClassDefFoundError: javax/annotation/PostConstruct
    at de.saxsys.mvvmfx.internal.viewloader.ViewLoaderReflectionUtils.lambda$initializeViewModel(ViewLoaderReflectionUtils.java:397)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1380)
    at de.saxsys.mvvmfx.internal.viewloader.ViewLoaderReflectionUtils.initializeViewModel(ViewLoaderReflectionUtils.java:395)
    at de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader.lambda$handleInjection[=12=](FxmlViewLoader.java:324)
    at de.saxsys.mvvmfx.internal.viewloader.ViewLoaderReflectionUtils.lambda$createAndInjectViewModel(ViewLoaderReflectionUtils.java:272)
    at de.saxsys.mvvmfx.internal.viewloader.ReflectionUtils.lambda$accessMember(ReflectionUtils.java:165)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at de.saxsys.mvvmfx.internal.viewloader.ReflectionUtils.accessMember(ReflectionUtils.java:161)
    at de.saxsys.mvvmfx.internal.viewloader.ViewLoaderReflectionUtils.createAndInjectViewModel(ViewLoaderReflectionUtils.java:264)
    at de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader.handleInjection(FxmlViewLoader.java:329)
    at de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader.access[=12=]0(FxmlViewLoader.java:48)
    at de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader$DefaultControllerFactory.call(FxmlViewLoader.java:311)
    at de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader$DefaultControllerFactory.call(FxmlViewLoader.java:286)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:938)
    at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.access00(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader.loadFxmlViewTuple(FxmlViewLoader.java:171)
    at de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader.loadFxmlViewTuple(FxmlViewLoader.java:82)
Disconnected from the target VM, address: '127.0.0.1:59447', transport: 'socket'
    at de.saxsys.mvvmfx.FluentViewLoader$FxmlViewStep.load(FluentViewLoader.java:333)
    at de.piepnitz.MaltPie.Main.startMvvmfx(Main.java:35)
    at de.saxsys.mvvmfx.guice.MvvmfxGuiceApplication.start(MvvmfxGuiceApplication.java:91)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:175)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: javax.annotation.PostConstruct
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    ... 41 more

提前致谢, 移液器

好的,我发现了问题:

对于 Java 9,javax.annotation 默认情况下不可见,因为它位于单独的模块中。由于 PostConstruct 是 javax.annotation 的一部分,因此不可用。

所以有以下解决方案:

  1. 将 Java 8 与 mvvmfx 一起使用。

  2. 添加模块

要添加模块,您可以执行以下操作:可以使用在 java.se.ee 模块中声明的 Java SE 和 Java EE 模块的混合集 当前 java.se.ee 包含在标准 JDK 9 中,但默认情况下未启用。可以通过“--add-modules”选项启用这组根模块。有关更多信息,请查看:LogicBic

当我需要 运行 特定供应商 Eclipse 插件的旧 Eclipse 版本时,我看到了这个错误。 我使用(在 Windows 中)一个 bat 文件临时将 Eclipse 的 PATH 更改为 运行 和 Java 8:

SETLOCAL
PATH={path_to_java_8_jre_or_java_8_jdk_bin_folder};%PATH%
eclipse.exe
ENDLOCAL

使用 Eclipse 文件夹设置此 bat 文件“开始于”的快捷方式,这样 Windows 将找到“eclipse.exe”。