Categories
Mastering Development

IllegalAccessException: cannot access class C (in module M) because module M does not export C to module N

I’m trying to add Maven support to an existing JavaFX project that I have.

I started by following OpenJFX’s Getting Started Guide, which teaches you how to compile and run a Hello World JavaFX application with Maven. After I made it, I pasted my project’s files in this new project and made the changes needed. My folder strucutre is more or less like this:

src/main/
  java/
    org.openjfx/
      Main.java
      components/
      controllers/
  resources/org.openjfx/fxml/

To run the project, I use these Maven commands:

compiler:compile
javafx:compile
javafx:run

However, I’m getting this error:

Exception in Application start method
Caused by: java.lang.IllegalAccessException: class javafx.fxml.FXMLLoader$ValueElement (in module javafx.fxml) cannot access class org.openjfx.controllers.LoginController (in module org.openjfx) because module org.openjfx does not export org.openjfx.controllers to module javafx.fxml

Which seems odd, since apparently I’m exporting the stuff in module-info.java:

module org.openjfx {
    requires javafx.controls;
    requires javafx.fxml;

    opens org.openjfx to javafx.fxml;
    exports org.openjfx;
}

I wonder if naming my package org.openjfx was a bad idea and may be causing issues, but I just followed along with the tutorial.

Below is my pom.xml file, if it’s of any relevance:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.openjfx</groupId>
    <artifactId>hellofx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>11</maven.compiler.release>
        <javafx.version>13</javafx.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                </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>
</project>

Leave a Reply

Your email address will not be published. Required fields are marked *