Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX lib can not be build any more since JavaFX 17 [duplicate]

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 801
eprevore Avatar asked Sep 09 '21 10:09

eprevore


People also ask

Does JavaFX work with Java 11?

For JDK 11 and later releases, Oracle has open sourced JavaFX. You can find more information at OpenJFX project.

Is JavaFX included in Java 7?

JavaFX and Java SE versions Starting with JavaFX 2.2 and Java SE 7 update 6 (7u6), the JavaFX libraries are part of Oracle JRE 7.

Does OpenJDK 11 support JavaFX?

JavaFX crashes with OpenJDK 11 on Ubuntu 18.04 with Wayland The recommended workaround is to use the Xorg server instead of the Wayland server when running JavaFX applications. Note that Wayland is not supported by JDK 10 or JDK 11.

What are the different libraries of JavaFX?

Different Libraries of JavaFX 1 ControlsFX. An open-source project that is used to offer high-quality controls on User Interface is present in JavaFX. 2 BootstrapFX. In JavaFX, a partial port of Twitter Bootstrap is available which is termed as BootstrapFX. ... 3 Object Graph Visualizer. ... 4 FormsFX. ... 5 RichTextFX. ... 6 FlexGanttFX. ...

Why can’t I run JavaFX from the tool window?

If your Operating System’s JAVA_HOME is lower than Java 15 (for example, mine’s set to Java 11), you’ll get an error trying to run javafx:run from the tool window: It may say something along the lines of "Unsupported major.minor version". This is an issue mentioned in the OpenJFX Getting Started Documentation.

Can I use Java module system in my JavaFX application?

I used Java 15 as the project SDK. However, it should also run with JDK 11 or higher. If you’re not familiar with the Java Module System, you may not want to use it in your JavaFX application. If that’s the case, read on as there are some caveats and workarounds you need to be aware of. Example application code here.

Can I use JavaFX with IntelliJ IDEA?

JavaFX has been through exciting times over the last five years or so, and it’s nice to see we can use Java on the front end as well as for an application’s back end. If you’re using a JavaFX front end for your application, you can build and run it in IntelliJ IDEA if it’s correctly configured in your selected build tool.


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 144
jewelsea Avatar answered Oct 08 '22 09:10

jewelsea