使用fxmlloader时发生illegalaccesserror

zbsbpyhn  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(337)

我正在尝试将一个fxml文件作为根小部件加载到javafx应用程序中,我使用的是windows8.1和java11.04,javafxsdk13和eclipse2019 ide。我在网上搜索了一下,但什么也没找到。我的情况有点复杂,因为javafx在java11上不可用,所以我自己安装了它,花了很多时间才弄清楚如何使用它。
这是我试图运行的代码:

package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Test1.fxml"));
            Scene scene = new Scene(root);
            ;

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
        return ;
    }
    public static void main(String[] args) {
        launch(args); // we could also say Application.launch(args)
        System.out.println("Hello World!");
        // this will launch the javafx application!
        ;

        return ;
    } 
}

我要加载的fxml文件是“test1.fxml”,它与main.class位于同一目录中。
这是整个控制台输出:

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 @0xcdce601) 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 @0xcdce601
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at application.Main.start(Main.java:32)
    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.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 application.Main

还有一件事是,当我使用一个没有导入的javafx类时,我将光标放在上面,它给出了几种可能的解决方案。其中两个是:import com.sun.javafx.instance和javafx.instance。我不知道com.sun.javafx是从哪里来的,也不知道下载了什么包。我只知道它不起作用,我只是坐在javafx.旁边。我在eclipse市场上安装了e(fx)clipse3.6.0,从gluonhq网站下载了.jar文件,并将它们分配为一个库,我将其命名为“javafx”,并将其添加到我当前的项目中。我认为fxmloader试图做的是从com.sun.whatever加载一些需要的包,并且在尝试导入某些内容时返回错误。

rbl8hiat

rbl8hiat1#

因为Java11你需要添加
--模块路径c:[您的路径]\javafx-sdk-13\lib--添加模块javafx.controls,javafx.fxml
项目运行配置的运行参数

相关问题