java - 无法访问类 com.sun.javafx.util.Utils(在模块 javafx.graphics 中)-JavaFX 和 Eclipse

标签 java javafx scenebuilder gluon-desktop

我已关注 guide将 JavaFX 设置到 Linux 机器上。
首先我安装了 Java 11

asus@asus-pc:/usr/share/openjfx/lib$ java -version
openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu219.04.1)
OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu219.04.1, mixed
mode, sharing)
asus@asus-pc:/usr/share/openjfx/lib$ 

然后我从命令 sudo apt-get install openjfx 安装了 OpenJFX
 asus@asus-pc:/usr/share/openjfx/lib$ ls
 javafx.base.jar      javafx.graphics.jar  javafx.swing.jar
 javafx.controls.jar  javafx.media.jar     javafx.web.jar
 javafx.fxml.jar      javafx.properties    src.zip
 asus@asus-pc:/usr/share/openjfx/lib$ 

然后在 Eclipse 中创建了一个库。

enter image description here

然后我将它包含到我的java项目中。我尝试运行此代码:
package se.danielmartensson.start;

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{

    /*
     * Start the start(Stage front)
     */
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage front) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/JUSBPlotter/src/se/danielmartensson/fxml/front.fxml"));
        Scene scene = new Scene(root);
        front.setScene(scene);
        front.setTitle("Fracken");
        front.show();
    }

}

我已将运行配置更改为:

enter image description here

但是当我编译代码时。我得到这个错误:
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 @0x1ff6d2c7) 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 @0x1ff6d2c7
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at se.danielmartensson.start.Main.start(Main.java:20)
    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.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    ... 1 more
Exception running application se.danielmartensson.start.Main

问题:

有没有人知道如何解决这个错误?我之前已经设置了 JavaFX,但是这一次,它对我不起作用。

编辑 1:
如果我将运行配置更改为:
--module-path="/usr/share/openjfx/lib" --add-modules=javafx.controls,javafx.fxml

我得到这个错误
enter image description here

最佳答案

IntelliJ IDEA 2020.1 与 JDK 14 存在相同的问题。
最后通过添加 module-info.java 解决喜欢 src/main/java 下的这个如果你使用 Maven:

module sample {
    requires javafx.controls;
    requires javafx.graphics;

    opens sample;
}

关于java - 无法访问类 com.sun.javafx.util.Utils(在模块 javafx.graphics 中)-JavaFX 和 Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56307973/

相关文章:

java - 弯曲 : add/edit//delete from database

java - Bouncy CaSTLe 密码学 API 可以在 Android 中使用吗?

java - 仅使用 FXML 为 JavaFX 2 按钮设置样式 - 如何将图像添加到按钮?

javafx - (在javaFx上)是否可以设置Controls Fx :id One element of that Control Array in Scene Builder?

java - tcp 中的 IOUtils.copy()

java - Junit 测试填充数组并计算无法填充的项目的方法

javafx - 为什么禁用阶段可调整大小在 javafx 中不起作用?

JavaFX TableView NullPointerException

mysql - JavaFX 文本字段未使用 MYSQL 查询更新

scroll - 如何在调整大小时使 flowpane 滚动