使用FXMLLoader时发生IllegalAccessError [英] IllegalAccessError while using FXMLLoader

查看:96
本文介绍了使用FXMLLoader时发生IllegalAccessError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将FXML文件作为我的根窗口小部件加载到我的javafx应用程序中,我正在将Windows 8.1与Java 11.04和javafx-sdk 13与Eclipse 2019 IDE一起使用.我在互联网上搜索了一下,但没有找到任何东西.我的情况有点复杂,因为javafx在Java 11上不可用,所以我自己安装了它,这花了很多时间来弄清楚该怎么做.

I am trying to load an FXML file into my javafx application as my root widget, I am using windows 8.1 with java 11.04 and javafx-sdk 13 with the Eclipse 2019 IDE. I searched a little bit on the internet but did not find anything. my situation is a bit complicated, because javafx is not available on java 11, so I installed it by myself which took a lot of time to figure out how to.

这是我要运行的代码:

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在同一目录中.

The fxml file that I want to load is "Test1.fxml" which in in the same directory with the 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)clipse 3.6.0,并从gluonHQ网站下载了* .jar文件,并将它们全部分配为一个名为"javafx"的库,并将其添加到当前项目中.我认为FXMLLoader试图从 com.sun.WhatEver 中加载一些所需的包,并且在尝试导入某些东西时返回错误.

One more thing is that when I use a javafx class that is not imported, I put the cursor and it suggests several possible solutions. Two of them are : import com.sun.javafx.Instance and javafx.Instance. I don't know where com.sun.javafx did come from neither what thing did download that package. All I know is that it is not working, I only sat up the javafx.* by my hands. I installed e(fx)clipse 3.6.0 on the market place of eclipse, and downloaded the *.jar files from gluonHQ website and assigned them all as one library that I named "javafx" and added it to my current project. What I think FXMLLoader is trying to do is loading some needed packages from com.sun.WhatEver and it is returning an error while trying to import something.

推荐答案

自Java 11开始,您需要添加

since Java 11 you need to add

-module-path C: [您的路径] \ javafx-sdk-13 \ lib --add-modules javafx.controls,javafx.fxml

--module-path C:[your path to]\javafx-sdk-13\lib --add-modules javafx.controls,javafx.fxml

进入项目运行配置的运行参数

to the run arguments of the project run configuration

这篇关于使用FXMLLoader时发生IllegalAccessError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆