11

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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
eprevore
  • 111
  • 1
  • 4
  • Check the run config (if you didnt explicitly create one, intellij generated a temp one for you when you clicked the green play button). One of the options in there tells intellij how to construct the classpath. Might be wrong – Michael Sep 09 '21 at 11:02
  • Can you try reloading the maven project? There should be a `Maven` tab om the right that includes a reload button. – dan1st Sep 09 '21 at 11:06
  • @Michael It looks like the run config is just set up with default values; working directory is the project's location, and it's not being passed any arguments or environment variables. Is it normally required to manually add the package locations to the run config? – eprevore Sep 09 '21 at 11:11
  • @dan1st I can reload the project w/no problems, but it doesn't fix the issue or change anything (as far as I can tell). – eprevore Sep 09 '21 at 11:13
  • What jappens if you run `mvn clean compile`? – dan1st Sep 09 '21 at 11:15
  • @dan1st it shows 18 errors with `compile`, although I can't expand them. The 14 errors with *App* (Main class) are still there, but now all the `cannot find symbol` errors only say "cannot find symbol" (not followed by a class or method) – eprevore Sep 09 '21 at 11:22
  • "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?" -> Please edit the question to add a link to a reference where you got this understanding. – jewelsea Sep 09 '21 at 11:33
  • Likely duplicate: [IntelliJ can't recognize JavaFX 11 with OpenJDK 11](https://stackoverflow.com/questions/52467561/intellij-cant-recognize-javafx-11-with-openjdk-11) – jewelsea Sep 09 '21 at 11:35
  • In linked question, see: "Before you run the default project, you just need to add these to the VM options: `--module-path /Users//Downloads/javafx-sdk-11/lib --add-modules=javafx.controls,javafx.fxml`" – jewelsea Sep 09 '21 at 11:36
  • Also for maven section, "Run `mvn compile javafx:run`, and it should work.", my guess is that you aren't running the app, that way (e.g. by right-clicking on the maven javafx:run goal), but are instead running the app using the default run configuration in idea (e.g. by right clicking on a class and running it). That is just speculation on my part, the later default configuration likely wouldn't work without a module-info I believe. – jewelsea Sep 09 '21 at 11:38
  • Also see [this answer to the same question](https://stackoverflow.com/a/57897944/1155209), which provides further context. – jewelsea Sep 09 '21 at 11:44
  • @jewelsea OK, added the JavaFX doc where I got that info from. You are correct about running the app, I wasn't aware there was another way to run it! I'll try a few different combinations of VM options and run methods. – eprevore Sep 09 '21 at 11:48
  • Yeah, in my opinion, that section that reads: "The JavaFX classes will be recognized.", should really read "The JavaFX classes will be recognized in the editor.". Because it doesn't really work at runtime unless you either add the correct `module-info.java` file or the correct `--add-modules` jvm options or run via the maven javafx runtime plugin (the later you would only do for development, never for a real deployment). I agree the situation is all quite confusing (and unfortunate), especially when starting out development . . . so many questions like this . . . – jewelsea Sep 09 '21 at 11:54
  • That second answer was definitely helpful in understanding the problem, unfortunately it seems like none of the alternate run methods were able to access the packages. The maven runtime plugin, both via the GUI and via `mvn compile javafx:run`, throws the same errors; appending the `--add-modules` options also has no effect (I suspect this could be because I'm specifying the module path incorrectly: I'm passing `D:\javafx-sdk-11.0.2\lib`). – eprevore Sep 09 '21 at 12:10
  • What happens if you follow [Intellij's instructions for a new JavaFX project](https://www.jetbrains.com/help/idea/javafx.html#troubleshoot)? Does it work? – jewelsea Sep 09 '21 at 12:23
  • I ended up creating a `module-info.java` just as a test. Made it require all the imported packages, as well as setting it to open the project's main package. No change in errors with that method either – eprevore Sep 09 '21 at 12:23
  • 1
    @jewelsea yep, creating a new JavaFX project works just fine. Interestingly, the JavaFX package dependencies in that project's pom.xml are of version **16** instead of the docs' recommended version **17** – eprevore Sep 09 '21 at 12:28
  • Version 17 is very new, it was only released a few days ago, so it is not surprising that the JavaFX new project wizard in Idea would not reference it by default. Likely if you update the generated pom.xml to 17, it will still work as the versions are largely interchangable. – jewelsea Sep 09 '21 at 12:43
  • 2
    After a bit more troubleshooting, I think I've determined that the JavaFX version actually *was* the problem. Changing the pom.xml in my main project to depend on version 16 immediately fixed all the package errors and ran the program successfully. Not sure why this is, I assume it must have something to do with the way JavaFX is bundled (or similar). – eprevore Sep 09 '21 at 21:35
  • See also: https://stackoverflow.com/questions/69128172/javafx- – jewelsea Sep 10 '21 at 10:51
  • So update OpenJFX and javafx plugin versions. – Joop Eggen Sep 10 '21 at 12:56

1 Answers1

14

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:

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

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 
    
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • JavaFX 17.0.0.1 fixed the issue, however `maven-assembly-plugin` logs a warning for every module (i.e.: *Failed to build parent project for org.openjfx:javafx-controls:jar:17.0.0.1*). I know it's just a warning and the build doesn't fail, but do you know why is logging this warning? – Oboe Sep 24 '21 at 21:09
  • @Oboe You could always create a [mcve] and ask your question as a new question if you wish. – jewelsea Sep 24 '21 at 21:35
  • It's a modular project with JavaFX custom controls (not an app with an executable JAR). I'll try to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and ask a new question. Not sure I'll be able to make it minimal though ;) – Oboe Sep 25 '21 at 00:08