2

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.

Josh S.
  • 101
  • 1
  • 6
  • Within an hour is quick for me I guess. I edited the post though. @ScaryWombat – Josh S. Nov 15 '21 at 01:25
  • 1
    If you put the full error messages formatted as multi-line code text in the question (not as screenshots), as well as the command used to run the app (also formatted as code) it is easier to help troubleshoot issues. – jewelsea Nov 15 '21 at 03:52
  • no screenshots of plain text please – kleopatra Nov 15 '21 at 05:10
  • @jewelsea, I did put the error message as text, it's the first code box. – Josh S. Nov 15 '21 at 15:01
  • But you didn’t put the text of the command used to run the app, which was the crucial thing for being able to understand and answer this question. The full command line is displayed by idea in the [console tab](https://www.jetbrains.com/help/idea/version-control-tool-window-console-tab.html) when you run the program, you can copy text from there, you don’t need to screenshot it and the screenshot does not display the full command line which was used, so it doesn’t help understand or solve the problem. – jewelsea Nov 15 '21 at 16:02
  • @jewelsea I see thank you. – Josh S. Nov 15 '21 at 17:15

2 Answers2

2

Okay, so I found out why this is occurring.

I put the module path and add modules in the program arguments and not in the vm options, which is what is causing the red issue.

Now I am encountering a "Error occurred during initialization of boot layer" issue now.

Josh S.
  • 101
  • 1
  • 6
  • Suggest you close this question given you have resolved it and post another if you are still having an issue – sprinter Nov 15 '21 at 02:05
  • Unfortunately, I can't resolve my own question with an answer for two days. I also have to wait 90 minutes to post another question. – Josh S. Nov 15 '21 at 02:12
  • You might like to take a look at my answer at https://stackoverflow.com/questions/66630618/intellij-ide-how-to-add-sdk/66630770#66630770 I have found this to be a better way to add JavaFX to a project than altering the VM options - and more in line with the expected use of java modules – sprinter Nov 15 '21 at 02:18
  • Close this question if you found the solution – Harry Coder Nov 15 '21 at 02:32
  • 4
    @HarryCoder No, the question does not need to be closed. It can be [self-answered](https://stackoverflow.com/help/self-answer). And I believe the current self-answer will be useful for future developers who may well experience the same issue. After a couple of days, when the system allows it, the self-answer can be marked correct by Josh S. – jewelsea Nov 15 '21 at 03:34
  • For thee boot layer initialization issue, probably your module path is wrong, see [Error occurred during initialization of boot layer java.lang.module. FindException: Module javafx.controls not found](https://stackoverflow.com/a/66287593/1155209). Looking at the module path used in your question, perhaps it needs to be absolute rather than relative e.g. `/Users/...` instead of `Users/...`. – jewelsea Nov 15 '21 at 03:49
1

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.

parsecer
  • 4,758
  • 13
  • 71
  • 140