Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX runtime components are missing, even though I already have them in my VM options and Modules

Tags:

java

javafx

I am getting this error:

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

Process finished with exit code 1

I checked multiple solutions on YouTube and StackOverflow such as https://www.youtube.com/watch?v=KKI7tDozPog and Error: JavaFX runtime components are missing, and are required to run this application with JDK 11

As a result, I followed their advice and added what they said to my VM options. This is what I have in my run configuration

enter image description here

Despite this however, I still encountered this error.

enter image description here

Here are some code that I have:

My pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>proj4</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>proj4</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.7.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17-ea+11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17-ea+11</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.example.proj4/application.proj4.PizzeriaApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

My application

package application.proj4;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;

public class PizzeriaApplication extends Application
{
    /**
     * This method is the start of the application.
     * @param stage A Stage object
     * @throws IOException An IOException object
     */
    @Override
    public void start(Stage stage) throws IOException
    {
        FXMLLoader fxmlLoader = new FXMLLoader(PizzeriaApplication.class.getResource("pizzeria-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 800, 600);
        stage.setTitle("RU Pizzeria");
        stage.setResizable(false);
        stage.setScene(scene);
        stage.show();

        //This closes the entire application if the Main Menu window is closed
        stage.setOnCloseRequest(t ->
        {
            Platform.exit();
            System.exit(0);
        });
    }

    /**
     * This method launches the application.
     * @param args An array of Strings
     */
    public static void main(String[] args)
    {
        launch();
    }
}

My dependencies in modules of project structure

enter image description here

A response is appreciated.

like image 503
Josh S. Avatar asked Oct 25 '25 04:10

Josh S.


1 Answers

2023, Answer that will work, for Idea Intellij (will work almost anywhere, very robust):

tl;dr:

  1. You need JavaFX jars. There is no way around that.
  2. You download the jars, using Maven
  3. You add the jars to the Intellij project
  4. You add the jars to VM options

Use Maven to download those jars:

pom.xml (some of the stuff there might be redundant, but it works and I don't want to risk it not working for you, so I'd rather post the full file that worked for me):

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>ConquerJavaFx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>ConquerJavaFx</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.9.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>20-ea+4</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>20-ea+4</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>ConquerJavaFx</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>

           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <excludeTransitive>false</excludeTransitive>
                    <stripVersion>false</stripVersion>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Add LIB folder to classPath -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

mvn install would install the JavaFx depedencies (the <dependencies> tag contains the info that will be used at this step).

Then do mvn package (the <build> tag contains the info that will be used at this step - the plugin that will copy all depedency jars into the target/lib folder).

It will create a lib folder with JavaFx jars, like this:

project root
  src 
    ...
  target
    classes
      ...
    lib
      javafx-base.jar
      javafx-controls.jar
      ...

Then in your project go File -> Project Structure -> Libraries -> plus sign + -> java -> go to the target/lib and select all the jars:

enter image description here

Now your source code will compile.

However JavaFX devs are evil,so it still won't run (will be the error about runtime components missing). So you need to do this:

Run -> Edit Configurations -> Modify options:

Also the proper Edit Configurations menu won't appear until you try to run the main class (and fail, since it's JavaFX we're deadling with...)

enter image description here

enter image description here

Then select Add VM options:

enter image description here

VM options input field will appear:

enter image description here

and enter

--module-path "../target/lib" --add-modules javafx.controls,javafx.fxml

where ../target/lib is the full path where your JavaFx jars are located.

in that field

Now it should run! I hope I saved you hours of frustration.

Polite words can't describe my emotions about the person who made the decision to remove JavaFX from Java!

Notes:

I tested in in a project without module-info.java

And the project in Idea Intellij was created as the simple one:

File - New -> Project -> New Project:

enter image description here

and my code in Intellij looks like this:

src (blue folder, sources root)
  somepackage1
     some codes
  somepackage2
     some codes
  Main.java (contains the start and the main method)

So the Main class is not in any package. I don't know how important it is, but considering the minefield that is making JavaFx to run, I'd rather let you know that.

like image 162
parsecer Avatar answered Oct 26 '25 17:10

parsecer