为什么Parent根=FXMLLoader不能正常工作?

huangapple go评论73阅读模式
英文:

Why Parent root =FXMLLoader is not working properly?

问题

以下是您提供的代码的翻译:

我正在尝试使用JavaFX构建一个应用程序但我遇到了问题
下面的代码生成错误自从我创建了这个项目以来就一直显示错误
我从"https://gluonhq.com/products/javafx/"下载了JavaFX 14并包括lib目录中的所有jar文件

但是这段代码可以正常工作

获取以下错误信息

C:\Program Files\Java\jdk-14.0.1\bin\java.exe --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\lib\idea_rt.jar=60984:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\Shapes\out\production\Shapes;E:\Software\javafx-sdk-14.0.2.1\lib\javafx-swt.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.controls.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.fxml.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.media.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.swing.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.web.jar -p E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar sample.Main
应用程序启动方法中的异常
java.lang.reflect.InvocationTargetException
    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    在 java.base/java.lang.reflect.Method.invoke(Method.java:564)    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    在 java.base/java.lang.reflect.Method.invoke(Method.java:564)    在 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)由于以下原因引发异常
java.lang.RuntimeException: Exception in Application start method
    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)    在 java.base/java.lang.Thread.run(Thread.java:832)由于以下原因引发异常
java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (在未命名模块 @0x66dac2b5) 无法访问类 com.sun.javafx.util.Utils (在模块 javafx.graphics) 因为模块 javafx.graphics 未将 com.sun.javafx.util 导出到未命名模块 @0x66dac2b5    在 com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)    在 javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)    在 sample.Main.start(Main.java:14)    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)    在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)    在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)    在 java.base/java.security.AccessController.doPrivileged(AccessController.java:391)    在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)    在 javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)    在 javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)    在 javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)    ... 1 more
应用程序运行时出现异常

进程退出代码为 1
英文:

I am trying to build an app in JavaFX but i am facing the issue.
Below code generating error.I error are showing since i created the project.
I downloaded the JavaFX 14 from "https://gluonhq.com/products/javafx/" and include all jar files present in lib directory.


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource(&quot;sample.fxml&quot;));
primaryStage.setTitle(&quot;Hello World&quot;);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

But This code works fine

package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
//Parent root = FXMLLoader.load(getClass().getResource(&quot;sample.fxml&quot;));
StackPane root=new StackPane();
primaryStage.setTitle(&quot;Hello World&quot;);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Getting this error:

&quot;C:\Program Files\Java\jdk-14.0.1\bin\java.exe&quot; --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED &quot;-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\lib\idea_rt.jar=60984:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\bin&quot; -Dfile.encoding=UTF-8 -classpath E:\Shapes\out\production\Shapes;E:\Software\javafx-sdk-14.0.2.1\lib\javafx-swt.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.controls.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.fxml.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.media.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.swing.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.web.jar -p E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar sample.Main
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:564)
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:564)
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:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x66dac2b5) 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 @0x66dac2b5
at com.sun.javafx.fxml.FXMLLoaderHelper.&lt;clinit&gt;(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.&lt;clinit&gt;(FXMLLoader.java:2056)
at sample.Main.start(Main.java:14)
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(AccessController.java:391)
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
Process finished with exit code 1

答案1

得分: 1

以下是翻译好的部分:

"fundamental error" 的基本错误是 (为了可读性格式化):

Caused by: java.lang.IllegalAccessError: 
    class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x66dac2b5) 
    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 @0x66dac2b5

该错误告诉您,javafx.fxml 模块已经出现在类路径上,而 javafx.graphics 模块位于模块路径上。这种情况阻止了 javafx.graphics 模块授予 javafx.fxml 模块的对内部代码的特殊访问权限。

解决此问题的方法之一是执行以下操作:

  1. javafx.fxml 包含在您的 --add-modules VM 参数中。
  2. 使您自己的代码成为模块化,添加必要的 requiresexportsopens 指令,并使用 --module 启动应用程序。

如果您对上述错误的产生方式感到好奇,那么 我的另一个答案 对此进行了更详细的解释。请注意,它侧重于 javafx.media 模块,但概念是相同的。

英文:

The fundamental error is (formatted for readability):

Caused by: java.lang.IllegalAccessError: 
    class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x66dac2b5) 
    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 @0x66dac2b5

That error is telling you that the javafx.fxml module has ended up on the class-path while the javafx.graphics module is on the module-path. This scenario prevents the special access to internal code the javafx.graphics module grants to the javafx.fxml module.

The solution to this is to do one of the following:

  1. Include javafx.fxml in your --add-modules VM argument.
  2. Make your own code modular, add the necessary requires, exports, and opens directives, and launch your application with --module.

If you're curious about how the above error comes about then my answer to another question goes into more detail. Note it focuses on the javafx.media module but the concept is the same.

huangapple
  • 本文由 发表于 2020年8月7日 19:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63301170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定