[英]Mvvmfx: initialize-method causes NoClassDefFoundError: javax/annotation/PostConstruct
i'm trying to implement a mvvm-pattern with JavaFx.我正在尝试使用 JavaFx 实现 mvvm-pattern。 To do so i'm using the mvvmfx-framework.
为此,我正在使用 mvvmfx 框架。
To use data from a model i've tried to use the NotificationCenter.为了使用来自模型的数据,我尝试使用 NotificationCenter。 When inserting an initialize-method in my viewModel to subscribe to messages i get an Exception: NoClassDefFoundError: javax/annotation/PostConstruct .
在我的 viewModel 中插入初始化方法以订阅消息时,我收到一个 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:这里是 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提前致谢,皮埃佩特
Okay, i found the problem:好的,我发现了问题:
With Java 9, javax.annotation
isn't visible by default as it's in a separate module.在 Java 9 中,默认情况下
javax.annotation
不可见,因为它位于单独的模块中。 Since PostConstruct is part of javax.annotation, it isn't available.由于 PostConstruct 是 javax.annotation 的一部分,因此它不可用。
So there are the following solutions:所以有以下解决方案:
Use Java 8 with mvvmfx.将 Java 8 与 mvvmfx 结合使用。
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.要添加模块,您可以执行以下操作: 可以使用
java.se.ee
模块中声明的 Java SE 和 Java EE 模块的混合集 目前java.se.ee
包含在标准 JDK 9 中,但未通过默认。 It's possible to enable this set of root modules via '--add-modules' option.可以通过“--add-modules”选项启用这组根模块。 For more information, check out: LogicBic
有关更多信息,请查看: LogicBic
I see this error when I need to run an old Eclipse version for specific vender Eclipse plugins.当我需要为特定供应商 Eclipse 插件运行旧的 Eclipse 版本时,我会看到此错误。 I use (in Windows) a bat file that changes the PATH temporarily for Eclipse to run with Java 8:
我使用(在 Windows 中)一个 bat 文件,它临时更改了 PATH 以便 Eclipse 与 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".使用 Eclipse 文件夹设置此 bat 文件“Start In”的快捷方式,以便 Windows 找到“eclipse.exe”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.