openjdk-jfx icon indicating copy to clipboard operation
openjdk-jfx copied to clipboard

FXMLLoader.load throws java.lang.IllegalAccessError

Open sleepynight2024 opened this issue 6 years ago • 9 comments
trafficstars

I am using Intellij (JavaFX with Maven module), after adding the javafx plugins I get the following error: 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:567)
	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:567)
	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:835)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x36cbb069) 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 @0x36cbb069
	at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
	at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
	at app.Main.start(Main.java:24)
	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(AccessController.java:389)
	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 app.Main

it is happening on: Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml")); // The sample.fxml is in the resources folder Here is my maven pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>Pere</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>org.openjfx.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>12.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>12.0.2</version>
        </dependency>
    </dependencies>



</project>

sleepynight2024 avatar Aug 01 '19 18:08 sleepynight2024

The following is suspicious:

Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x36cbb069) 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 @0x36cbb069

The FXMLLoaderHelper class should be coming from the javafx.fxml module not the unnamed module.

@jperedadnr or @abhinayagarwal might be able to comment on this.

kevinrushforth avatar Aug 02 '19 12:08 kevinrushforth

The issue here is with how IntelliJ IDEA handles a Java application with JavaFX dependencies. The following java command is executed by IntelliJ IDEA when a JavaFX 11+ application with OP's pom.xml is executed:

java --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED ... -p ~/.m2/repository/org/openjfx/javafx-base/12.0.2/javafx-base-12.0.2-linux.jar:~/.m2/repository/org/openjfx/javafx-graphics/12.0.2/javafx-graphics-12.0.2-linux.jar org.openjfx.App

It adds javafx.base and javafx.graphics to the module-path and leaves out javafx.controls and javafx.fxml on the classpath, causing all the confusion.

Solutions

  1. Add the missing modules manually to the module-path. Check this SO answer from @jperedadnr
  2. Run javafx:run goal from IntelliJ Maven window instead of running the Application class

abhinayagarwal avatar Aug 02 '19 14:08 abhinayagarwal

Also faced this issue. Should we create a bug on Idea bugtracker?

Gwulior avatar Mar 27 '20 13:03 Gwulior

I think we should

WEshalom avatar Aug 12 '20 21:08 WEshalom

Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class checkersFX.Main (in module CheckersFX) because module CheckersFX does not export checkersFX to module javafx.graphics at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376)

WEshalom avatar Aug 12 '20 21:08 WEshalom

Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class checkersFX.Main (in module CheckersFX) because module CheckersFX does not export checkersFX to module javafx.graphics

This is not the same issue discussed above. It is an application error. See https://openjfx.io/javadoc/14/javafx.graphics/javafx/application/Application.html and look for "Deploying an Application as a Module".

kevinrushforth avatar Aug 12 '20 22:08 kevinrushforth

Thank you

WEshalom avatar Aug 20 '20 21:08 WEshalom

Caused by: java.lang.IllegalAccessException: class javafx.fxml.FXMLLoader$ValueElement (in module javafx.fxml) cannot access class UXUI.MainMenuController (in module src) because module src does not export UXUI to module javafx.fxml

Really not sure what's going on here, any help appreciated!

Unays7 avatar Dec 27 '20 22:12 Unays7

Hey ! I fixed this problem by adding the configuration parameters : --module-path "your path\javafx-sdk-15.0.1\lib" --add-modules=javafx.controls,javafx.fxml

Er4g00n avatar Jan 18 '21 15:01 Er4g00n