首先我使用 eclipse 2024-03 和最新的 javafx SDK。
我有一些 javaFX 代码,当它位于创建新的 javafx 项目时自动创建的应用程序包中时,它们可以工作。但是当我在同一个项目中创建一个新包并尝试运行相同的代码时,我收到以下错误:
boot层初始化时发生错误 java.lang.module.FindException:模块 javafx。没有找到
有人可以告诉我需要为该特定包更改哪些设置吗?
编辑:再次尝试后我收到这些错误:
Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1085)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class zwischenuebungen.LayoutContainer
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:883)
at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class zwischenuebungen.LayoutContainer (in module Test1) because module Test1 does not export zwischenuebungen to module javafx.graphics
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:795)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
这个问题与你的
module-info.java
有关。
可能有一行
exports <somepackagename>
,其中 <somepackagename>
不是 zwischenuebungen
,这是您要使用的包。
您需要
export
或 open
适合您的申请的软件包。
错误消息告诉您需要打开什么包以及需要打开该包到哪个模块:
module Test1 does not export zwischenuebungen to module javafx.graphics
因此,最小的
module-info.java
(您的实际应用可能需要更多声明)是:
module Test1 {
requires javafx.controls;
exports zwischenuebungen;
}
这为
zwischenuebungen
包的所有公共成员提供了对其他模块的完全可见性。
或者直接打开
javafx.graphics
:
module Test1 {
requires javafx.controls;
opens zwischenuebungen to javafx.graphics;
}
这限制了指定模块的可见性。
需要这样做的原因是
javafx.graphics
模块正在使用反射来实例化和启动您的JavaFX应用程序,并且需要包含该应用程序的包打开javafx.graphics
模块才能工作。
Application
javadoc中提到:
将应用程序部署为模块
如果应用程序子类位于命名模块中,则该类必须 可访问
模块。否则,异常 当应用程序启动时将抛出。这意味着在 除了类本身被声明为公共之外,模块还必须 导出(或打开)包含的包至少到javafx.graphics
模块。javafx.graphics
例如,如果
在com.foo.MyApplication
模块中,则foo.app
可能看起来像这样:module-info.java
module foo.app { exports com.foo to javafx.graphics; }