简体   繁体   English

从 IntelliJ 运行 JavaFX 应用程序

[英]Running JavaFX Application from IntelliJ

Versions版本

openjdk version "11.0.11" 2021-04-20 openjdk 版本“11.0.11” 2021-04-20

OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.10) OpenJDK 运行环境(build 11.0.11+9-Ubuntu-0ubuntu2.20.10)

OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.10, mixed mode, sharing) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.10, 混合模式, 共享)

IntelliJ IDEA 2021.1.2 (Ultimate Edition) Build #IU-211.7442.40, built on May 31, 2021 IntelliJ IDEA 2021.1.2(终极版)Build #IU-211.7442.40,构建于 2021 年 5 月 31 日

JavaFX SDK 11.0.2 JavaFX SDK 11.0.2

Hey guys, first I want to say that this question was somewhat asked before here I followed all the steps that the correct answer gives, and end up with this error:嘿伙计们,首先我想说的是,在之前有人问过这个问题,我遵循了正确答案给出的所有步骤,并最终出现了这个错误:

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:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    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:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
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:831)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6c5aff76) 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 @0x6c5aff76
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at KiloConverter.start(KiloConverter.java:12)
    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.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)

I am somewhat of a beginner with Java, and I am doing this project for a college course.我是 Java 的初学者,我正在为大学课程做这个项目。 I can build the application, but cannot run it.我可以构建应用程序,但无法运行它。 Like I said, I followed all the steps the previous answer gave, to no avail.就像我说的那样,我遵循了前面答案给出的所有步骤,但无济于事。

Please if possible, don't tell me to "read up on the basics" like I saw other people answer before .如果可能,请不要像我之前看到其他人回答的那样告诉我“阅读基础知识”。 I just am not sure why my code won't run for my final project.我只是不确定为什么我的代码不会为我的最终项目运行。

Thank you so much in advance!非常感谢您!

Since you mention you're a beginner in Java I first recommend reading this Q&A: What is a stack trace, and how can I use it to debug my application errors?既然您提到您是 Java 的初学者,我首先建议您阅读此问答: 什么是堆栈跟踪,以及如何使用它来调试我的应用程序错误? . . Then when you look back at your stack trace you'll see the fundamental error you're getting is:然后,当您回顾堆栈跟踪时,您会看到您得到的基本错误是:

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

This is a module-related error.这是与模块相关的错误。 You may want to read Understanding Java 9 Modules to familiarize yourself with modules.您可能需要阅读了解 Java 9 模块以熟悉模块。

In this case the error occurs because the javafx.fxml module is being found on the class-path while the javafx.graphics module is being found on the module-path.在这种情况下,会发生错误,因为在类路径中找到javafx.fxml模块,而在模块路径中找到javafx.graphics模块。 In other words, the javafx.fxml module is failing to be resolved as a named module but its code is still being found in the unnamed module .换句话说, javafx.fxml模块无法解析为命名模块,但其代码仍可在未命名模块中找到。 This breaks the qualified exports and qualified opens directives that javafx.graphics may give to javafx.fxml and the result is the IllegalAccessError .这会破坏javafx.graphics可能给javafx.fxml的合格导出和合格打开指令,结果是IllegalAccessError

You need to make sure the javafx.fxml module is actually resolved as a module.您需要确保javafx.fxml模块实际上被解析为一个模块。 There are at least two ways to do this:至少有两种方法可以做到这一点:

  1. Include javafx.fxml in your --add-modules JVM argument.--add-modules JVM 参数中包含javafx.fxml

  2. Make your code modular by adding a module-info.java file:通过添加module-info.java文件使您的代码模块化:

     // or whatever you want to name your module module app { requires javafx.controls; requires javafx.fxml; // export your Application subclass's package to at least javafx.graphics exports com.example.app to javafx.graphics; // open your FXML controller package(s) to at least javafx.fxml (allows reflective access) opens com.example.app.controllers to javafx.fxml; }

    And launch your application as a module.并将您的应用程序作为一个模块启动。 For example, this is what it would look like on the command line:例如,这就是它在命令行上的样子:

     java --module-path <path> --module <module>/<main-class>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM