IllegalAccessError while using FXMLLoader

浪尽此生 提交于 2021-02-05 09:26:09

问题


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.

This is the code that I am trying to run :

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 ;
    } 
}

The fxml file that I want to load is "Test1.fxml" which in in the same directory with the Main.class .

This is the whole console output :

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

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.


回答1:


since Java 11 you need to add

--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



来源:https://stackoverflow.com/questions/58292087/illegalaccesserror-while-using-fxmlloader

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!