IntelliJ IDEA - 错误:缺少 JavaFX 运行时组件,并且需要运行此应用程序

2022-09-05 00:28:25

我正在运行IntelliJ IDEA Ultimate 2018.2.5JDK 11.0.1OpenJFX 11。我知道这是一个常见的错误,我尝试了许多建议的修复程序,但没有任何效果。

无论我尝试运行哪个JavaFX项目,我都会得到错误:

Error: JavaFX runtime components are missing, and are required to run this application

如果我将以下内容添加到 VM 选项

 --module-path="C:\Program Files\Java\javafx-sdk-11\lib" --add-modules=javafx.controls

我收到以下错误:

Exception in Application start method
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:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x5fce9dc5) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x5fce9dc5
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    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:174)
    ... 1 more
Exception running application sample.Main

我尝试重新安装,没有任何运气。我也尝试过更改为类似的东西,但仍然不起作用。getClass().getResource(...)getClass().getClassLoader().getResource(...)Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));


答案 1

有类似的问题,比如这个或这个其他的问题

在JavaFX 11之前,每当你调用与JavaFX相关的内容时,你都有SDK中可用的所有javafx模块。

但现在你必须包括你需要的模块/依赖项。

您的错误表明您正在使用FXML,但无法解决,但您刚刚添加了模块:javafx.controls

--add-modules=javafx.controls

正如您在 JavaDoc 中看到的,模块依赖于 和 ,但这些模块都不包含 FXML 类。javafx.controlsjavafx.graphicsjava.base

如果您需要像 这样的 FXML 类,则需要包含模块:FXMLLoaderjavafx.fxml

 --module-path="C:\Program Files\Java\javafx-sdk-11\lib" \
    --add-modules=javafx.controls,javafx.fxml

如果您需要媒体或webkit,它们也同样适用,它们有自己的模块


答案 2

推荐