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

Dan*_*son 16 java javafx scenebuilder gluon-desktop

我已按照本指南将 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$ 
Run Code Online (Sandbox Code Playgroud)

然后我从命令安装了 OpenJFX sudo apt-get install 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$ 
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

然后我将它包含到我的 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();
    }

}
Run Code Online (Sandbox Code Playgroud)

我已将运行配置更改为:

在此处输入图片说明

但是当我编译代码时。我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

题:

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

编辑 1: 如果我将运行配置更改为:

--module-path="/usr/share/openjfx/lib" --add-modules=javafx.controls,javafx.fxml
Run Code Online (Sandbox Code Playgroud)

我收到这个错误 在此处输入图片说明

igo*_*ack 11

使用 JDK 14 的 IntelliJ IDEA 2020.1 也有同样的问题。

如果你使用maven,最后通过在下面添加一个module-info.java这样的来解决src/main/java

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

    opens sample;
}
Run Code Online (Sandbox Code Playgroud)


小智 8

转到“运行”>“运行配置”,然后转到“参数”选项卡,然后转到“VM 参数”并粘贴以下代码以添加模块“--module-path /path/to/lib --add-modules javafx.controls,javafx.fxml”,记住修改/path/to/lib 到您的库的路径,然后单击“应用”即可设置


use*_*399 5

谢谢你。我按照你说的做了,但之后我遇到了一些其他异常,所以我想出了以下代码:

module {pkg}{
   requires javafx.controls;
   requires javafx.graphics;
   requires javafx.fxml;

   exports {pkg of Application class};

   opens {pkg};
}
Run Code Online (Sandbox Code Playgroud)

之后你需要重建你的项目,可能是因为 Kotlin 的一些异常。然后我看到异常“位置未设置”。要解决此问题,您必须以“/”开头 fxml 位置,例如:

App.class.getResource("/form.fxml");
Run Code Online (Sandbox Code Playgroud)

编辑

我使用 Gradle 在JavaFxHelloWorld上创建了一个 HelloWorld 项目。


小智 5

谢谢,对我来说如下

public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Run Code Online (Sandbox Code Playgroud)

模块信息.java:

module sample {
    requires javafx.controls;
    requires javafx.graphics;
    requires javafx.fxml;
    opens sample ;
}
Run Code Online (Sandbox Code Playgroud)