0

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

Piepette
  • 21
  • 3
  • Can you tell something about your environment? Which operating system and which JVM? This error tells you that the class `javax.annotation.PostConstruct` isn't available. However, this class is part of default JDK (https://docs.oracle.com/javase/8/docs/api/javax/annotation/PostConstruct.html) so it seems to be a problem with your JDK? – Manuel Mauky Feb 08 '18 at 13:19
  • Hello @ManuelMauky, yea i thought it could be a problem with the jdk, so i updated from 9.0.1 to 9.0.4. Unfortunately this didn't fix the problem. "C:\Program Files\Java\jdk-9.0.4\bin\java" I'm using Intellij on a Windows-Machine, building the project with maven. – Piepette Feb 09 '18 at 15:08
  • getting same error in openjdk 10.0.1 on Ubuntu 18.04 – Nikhil VJ Aug 11 '18 at 05:41

2 Answers2

0

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

Piepette
  • 21
  • 3
  • Is there anything that can be done in the mvvmfx framework to simplify this? If you know how to solve this in the framework I would highly appreciate a hint or even a pull-request. – Manuel Mauky Feb 16 '18 at 16:18
0

I see this error when I need to run an old Eclipse version for specific vender Eclipse plugins. I use (in Windows) a bat file that changes the PATH temporarily for Eclipse to run with Java 8:

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

Set your shortcut to this bat file "Start In" with the Eclipse folder so Windows will find "eclipse.exe".