Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ doesn't load javafx packages from Maven dependencies (JavaFX 17)

I'm trying to get a Maven/JavaFX project, created from the javafx-archetype-fxml archetype and unedited, to run in the latest version of IntelliJ. To be clear, the project is a direct copy of that archetype; I'm just trying to get an example working.

Suffice it to say I'm a complete beginner with Maven, so I'm could just be missing an obvious step here.

Maven build went smoothly, and the project's pom.xml looks the way the JavaFX documentation says it should.
I left it unchanged except for updating the maven.compiler.source and maven.compiler.target properties, as well as the release property in the maven-compiler-plugin, to 16, the JDK version I'm using for the project:

    <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>com.epre</groupId>
        <artifactId>jfx-sandbox</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>16</maven.compiler.source>
            <maven.compiler.target>16</maven.compiler.target>
        </properties>
        
        <dependencies>
            <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-controls</artifactId>
                <version>17</version>
            </dependency>
            <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-fxml</artifactId>
                <version>17</version>
            </dependency>
        </dependencies>
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <release>16</release>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-maven-plugin</artifactId>
                    <version>0.0.6</version>
                    <executions>
                        <execution>
                            <!-- Default configuration for running -->
                            <!-- Usage: mvn clean javafx:run -->
                            <id>default-cli</id>
                            <configuration>
                                <mainClass>com.epre.App</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>

The dependencies show up in the Maven tab, and I'm able to reload the project with no problems. Similarly, I can see that the javafx-base, -controls, -fxml, -graphics, and their corresponding :win libraries have been added to the project's External Libraries (pic):

However, when I try to run the project's Main class, IntelliJ throws ~15 errors telling me that many of the packages I'm trying to import from don't exist.

    java: package javafx.application does not exist  
    java: package javafx.fxml does not exist  
    java: package javafx.scene does not exist  
    java: package javafx.scene does not exist  
    java: package javafx.stage does not exist  
    java: cannot find symbol class Application  
    java: cannot find symbol class Scene  
    java: method does not override or implement a method from a supertype  
    java: cannot find symbol class Stage  
    java: cannot find symbol class Scene  
    java: cannot find symbol class Parent  
    java: cannot find symbol class FXMLLoader  
    java: cannot find symbol class FXMLLoader  
    java: cannot find symbol method launch()

This is the Main class, just to show what sort of packages I'm trying to import from:

    package com.epre;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    import java.io.IOException;
    
    /**
     * JavaFX App
     */
    public class App extends Application {
    
        private static Scene scene;
    
        @Override
        public void start(Stage stage) throws IOException {
            scene = new Scene(loadFXML("primary"), 640, 480);
            stage.setScene(scene);
            stage.show();
        }
    
        static void setRoot(String fxml) throws IOException {
            scene.setRoot(loadFXML(fxml));
        }
    
        private static Parent loadFXML(String fxml) throws IOException {
            FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
            return fxmlLoader.load();
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }

I've done some searching and tried fixing every unrelated problem with the project, just to try and isolate the issue, i.e.

  • Changing the project language level to 16
  • Changing the project module's Per-module bytecode version to 16
  • Replacing instances of "11" in the pom.xml with "16" as mentioned earlier
  • Opening the project in an older version of IntelliJ

None of these have produced any change, though.

In previous non-Maven projects that also used JavaFX, I had to add all the packages the project needed to its module-info.java. This is the only step I can think of that I haven't taken, since it's my understanding that I shouldn't have to deal with it if I'm declaring those packages as dependencies in the pom.xml?

EDIT: I'm assuming I don't need to set up a module-info.java because the JavaFX documentation never mentions it as a step for creating a Maven+JavaFK project: https://openjfx.io/openjfx-docs/.
The JavaFX and IntelliJ > Non-modular with Maven section of the documentation simply states that upon loading the project, "The JavaFX classes will be recognized."

EDIT 2: I managed to solve the package errors and get the program running by changing the JavaFX dependencies in the pom.xml to version 16 instead of 17. Not sure why a single version would break the program like it did, though I suspect there was probably a change in how JavaFX is bundled/distributed.

like image 725
eprevore Avatar asked Sep 09 '21 10:09

eprevore


People also ask

How to make a JavaFX project with Maven support in IntelliJ?

It really is amazingly easy to make a JavaFX project with Maven support in IntelliJ. In fact, you just have to do 4 simple things: Create a JavaFX project – IntelliJ does this for you! Add Maven support – IntelliJ does this too! We’ve scattered optional hints and tips throughout the article.

How do I import dependencies into IntelliJ Maven?

You can import dependencies to your Maven project. When IntelliJ IDEA imports the added dependency, it parses the dependency and updates your project. In the pom.xml file, add a dependency you need. When you change the pom.xml, IntelliJ IDEA displays a notification suggesting to load the changes.

How to run JavaFX application in Maven with illegalaccesserror?

In your Maven project you have the JavaFX plugin configured and JavaFX dependencies setup as per Jose's answer. You go to the source code of your main class which extends Application, you right-click on it and try to run it. You get an IllegalAccessError involving an "unnamed module" when trying to launch the app.

How do I package a JavaFX application?

You can package your JavaFX application by building the corresponding artifact. For JavaFX applications, IntelliJ IDEA provides a dedicated artifact type (JavaFx Application). One JavaFx Application artifact configuration is created by IntelliJ IDEA automatically if you create a project as described in Create a project for JavaFX development.


Video Answer


1 Answers

Update for JavaFX 17.0.0.1 release

The release of JavaFX 17.0.0.1 has resolved this issue and, when using JavaFX and Maven this is the version that should be used instead of 17 (which remains broken in Maven).

When defining a dependency on JavaFX 17 using Maven, ensure that the version defined for JavaFX dependencies is not 17 and is at least 17.0.0.1. Here is an example of a working dependency definition for JavaFX 17.0.0.1 Maven modules.

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>17.0.0.1</version>
</dependency>

Info on release versions and contents and the version number for the current latest release is available in the JavaFX release notes hosted at gluon.

For now, I will leave the rest of the original answer, which discusses some of the background information, as it was when it was originally created.

Maven modules for JavaFX versions prior to 17 (e.g. 16), still continue to function without issue. However, if you have the ability to upgrade and use JavaFX 17.0.0.1 or higher for your application, I encourage this.

JavaFX 17 will be maintained as a stable long-term release of JavaFX. It will maintain a stable feature set and receive bug and security fix support for many years.


Background

I was able to replicate this issue.

This is a known issue only affecting projects which rely on the initial JavaFX 17 release artifacts currently available in the Maven central repository.

See related question:

  • JavaFX lib can not be build any more since JavaFX 17

Discussion of the issue on the openjfx-dev mailing list:

  • https://mail.openjdk.java.net/pipermail/openjfx-dev/2021-September/031934.html

Workaround

One current workaround is, if your application relies on JavaFX artifacts from Maven central, to use JavaFX 16 rather than JavaFX 17 until this issue is fixed.

As this is quite a critical issue with the JavaFX 17 release, I would expect it will likely be addressed in an update to the JavaFX 17 release in the near future.

Environment

Mac OS (Catalina) 10.15.7

$ java -version
openjdk version "16.0.2" 2021-07-20
OpenJDK Runtime Environment Temurin-16.0.2+7 (build 16.0.2+7)
OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (build 16.0.2+7, mixed mode, sharing)

IntelliJ IDEA 2021.2 (Ultimate Edition)
Build #IU-212.4746.92, built on July 27, 2021

Steps to replicate

  1. In Idea create a new JavaFX Project
  • Select New | Project
  • Choose JavaFX in the left tab.
  • Choose Language: Java, Build System: Maven, Project SDK: 16
  • Select Next -> Finish
  1. Test the new project.
  • Right click on HelloApplication and select Run 'HelloApplication.main()'
  • The application should run and display a JavaFX application window with a "Hello!" button.
  1. Change the JavaFX version
  • Edit pom.xml
  • Change the JavaFX dependency versions from 16 to 17
  • maven-compiler-plugin version can stay at 16.
  • In the Maven tab, click the refresh icon to "Reload All Maven Projects"
  1. Test the updated project.
  • Right click on HelloApplication and select Run 'HelloApplication.main()'

  • Execution will now fail with the error message:

    java: package javafx.fxml does not exist 
    
like image 167
jewelsea Avatar answered Sep 20 '22 14:09

jewelsea