IntelliJ IDEA - 错误:缺少 JavaFX 运行时组件,需要运行此应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52906773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ IDEA - Error: JavaFX runtime components are missing, and are required to run this application
提问by abg
I'm running IntelliJ IDEA Ultimate 2018.2.5with JDK 11.0.1and JavaFX 11 from OpenJFX. I know it's a common error and I tried many of the proposed fixes but nothing works.
我跑的IntelliJ IDEA终极2018年2月5日与JDK 11.0.1和JavaFX的11从的OpenJFX。我知道这是一个常见错误,我尝试了许多建议的修复程序,但没有任何效果。
No matter which JavaFX project I try to run I get the error:
无论我尝试运行哪个 JavaFX 项目,我都会收到错误消息:
Error: JavaFX runtime components are missing, and are required to run this application
If I add the following to the VM options
如果我将以下内容添加到 VM 选项中
--module-path="C:\Program Files\Java\javafx-sdk-11\lib" --add-modules=javafx.controls
I get these errors:
我收到这些错误:
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(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(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(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(WinApplication.java:174)
... 1 more
Exception running application sample.Main
I tried reinstalling without any luck. I have also tried to change
getClass().getResource(...)
to getClass().getClassLoader().getResource(...)
or to something like Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
but still doesn't work.
我尝试重新安装但没有任何运气。我也尝试更改getClass().getResource(...)
为getClass().getClassLoader().getResource(...)
或更改
为类似的内容,Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
但仍然无效。
采纳答案by José Pereda
There are similar questions like thisor this other one.
Before JavaFX 11, whenever you were calling something JavaFX related, you had all the javafx modules available within the SDK.
在 JavaFX 11 之前,无论何时调用与 JavaFX 相关的内容,您都可以在 SDK 中使用所有 javafx 模块。
But now you have to include the modules/dependencies you need.
但是现在您必须包含您需要的模块/依赖项。
Your error says that you are using FXML but it can't be resolved, but you have just added the javafx.controls
module:
您的错误表明您正在使用 FXML,但无法解决,但您刚刚添加了javafx.controls
模块:
--add-modules=javafx.controls
As you can see in the JavaDocthe javafx.controls
module depends on javafx.graphics
and java.base
, but none of those modules includes the FXML classes.
正如你可以在看的JavaDoc的javafx.controls
模块依赖于javafx.graphics
和java.base
,但没有这些模块包括FXML类。
If you need FXML classes like the FXMLLoader
, you need to include javafx.fxml
module:
如果您需要像 一样的 FXML 类FXMLLoader
,则需要包含javafx.fxml
模块:
--module-path="C:\Program Files\Java\javafx-sdk-11\lib" \
--add-modules=javafx.controls,javafx.fxml
The same will apply if you need media or webkit, those have their own modules.
如果您需要媒体或 webkit,这同样适用,它们有自己的模块。