Mvvmfx:initialize-method导致NoClassDefFoundError:javax / annotation / PostConstruct

时间:2018-02-07 17:22:53

标签: java mvvm java-9 postconstruct mvvmfx

我试图用JavaFx实现mvvm模式。为此,我使用了mvvmfx-framework。

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

如果有人能告诉我自己做错了什么,我会感激不尽。

这是我的viewModel:

@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$2(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$9(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$0(FxmlViewLoader.java:324)
    at de.saxsys.mvvmfx.internal.viewloader.ViewLoaderReflectionUtils.lambda$createAndInjectViewModel$4(ViewLoaderReflectionUtils.java:272)
    at de.saxsys.mvvmfx.internal.viewloader.ReflectionUtils.lambda$accessMember$3(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$000(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.access$2700(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$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(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$3(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

提前致谢, Piepette

2 个答案:

答案 0 :(得分:0)

好的,我发现了问题:

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

所以有以下解决方案:

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

  2. 添加模块

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

答案 1 :(得分:0)

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

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

使用 Eclipse 文件夹设置此 bat 文件“Start In”的快捷方式,以便 Windows 找到“eclipse.exe”。