Search code examples
javamavenintellij-ideajavafx

JavaFX Maven project in IntelliJ. JavaFX runtime components are missing only in IntelliJ. JAR through mvn package executes without issues


I have a Java Project with Spring Boot and JavaFX added through maven. The code compiles and even i can execute the fat jar without the JavaFX SDK in the computer. But when I try to execute it in IntelliJ it results in

Error: JavaFX runtime components are missing, and are required to run this application

I have seen this output in many questions and in most of those cases the jar wasn't built at all or code compilation failed.

But in this scenario the mvn package works with no errors and I can execute the JAR with java -jar <jar_name> to cross out the fact that I might have the javafx sdk installed somewhere I tried it in a VM with only the JRE installed.

pom.xml

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

As for plugins spring-boot-maven-plugin and maven-compiler-plugin.

Attempted Solutions

--1--

I tried the solution which said to add the

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.6</version>
    <executions>
        <execution>
            <id>default-cli</id>
            <configuration>
                <mainClass>com.example.demofx.Starter</mainClass>
            </configuration>
        </execution>
    </executions>
</plugin>

But what it does is add the ability to run with: mvn clean javafx:run

The need to execute with IntelliJ is to debug the code because Debugging with souts isn't efficient.

--2--

Trying to module build with a module-info.jar with following

module com.example.demofx {
    requires javafx.controls;
    requires javafx.fxml;
    // all other required modules including spring


    opens com.example.demofx to javafx.fxml;
    exports com.example.demofx;
}

This might have worked but due to some of old dependencies not working properly with modularized build this results in lots of breaking changes to the codebase.

Edit:

  1. Missed to mention the environment
    • JDK - 11.0.8
    • IntelliJ IDEA - 2021.2.2
  2. Added second solution tried.

Solution

  • This is more a troubleshooting and research guide than an actual fix. Fixes for environmental issues are difficult to provide in a StackOverflow context. However, if you study the information here, it might help you fix your project.

    Recommended troubleshooting approach

    Use the Intellij new JavaFX project wizard. Ensure that it works in your environment, then gradually add components from your current project into the working project, checking that everything still works after each small addition.

    Debugging when executing via the JavaFX maven plugin

    I think the above recommendation is the preferred approach, however, you can alternately get the following to work:

    run with: mvn clean javafx:run The need to execute with IntelliJ is to debug the code"

    See:

    I also think you can just right-click on the maven target for javafx:run and select Debug. I am not sure, I don't make use of the JavaFX maven plugin.

    Creating fat jars for JavaFX applications

    the fat jar

    This is not a recommended configuration, but if you really must do it, you can review:

    That answer doesn't discuss getting such a configuration to work in conjunction with an Idea run/debug configuration, so it may not assist you.

    If you do continue with a fat jar, I would not advise using a module-info, as you will be running code off the classpath anyway.

    Modular versus non-modular JavaFX applications

    If you don't use a fat jar, getting all the module dependencies correct for Spring is tricky anyway because Spring is not currently architected to directly support modules well. Spring 6 will be designed to work well with modules, though I think you should be able to get Spring 5 to work if you try hard enough (I have got it to work in the past for some applications).

    Alternately you can just have the JavaFX components as modules and run the rest off the classpath. For example, the "Non-modular with Maven" approach at openjfx.io. Note that in that approach, the JDK and JavaFX modules are still loaded as modules off of the module path, it is only Spring your application that is not providing a module-info.java file and running off the classpath.

    Creating runtime images for JavaFX applications

    I also advise studying: