javamvvmjava-9postconstructmvvmfx

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


i'm trying to implement a mvvm-pattern with JavaFx. To do so i'm using the mvvmfx-framework.

To use data from a model i've tried to use the NotificationCenter. When inserting an initialize-method in my viewModel to subscribe to messages i get an Exception: NoClassDefFoundError: javax/annotation/PostConstruct.

I would be thankful if anybody could tell me what i'm doing wrong.

This is my 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

    }

}

This is where i start the application

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);
    }
}

And here goes the 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

Thanks in advance, Piepette


Solution

  • Okay, i found the problem:

    With Java 9, javax.annotation isn't visible by default as it's in a separate module. Since PostConstruct is part of javax.annotation, it isn't available.

    So there are the following solutions:

    1. Use Java 8 with mvvmfx.

    2. Add the module

    To add the module, you can do the following: It's possible to use the mixed set of Java SE and Java EE modules declared in java.se.ee module Currently java.se.ee is included in standard JDK 9 but is not enabled by default. It's possible to enable this set of root modules via '--add-modules' option. For more information, check out: LogicBic