24

I have downloaded JavaFX SDK, unpacked it and set a PATH_TO_FX system variable, following this instructions. I used following code example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Tried to compile it with the suggested pattern:

javac --module-path $PATH_TO_FX --add-modules javafx.controls HelloFX.java

But compiler throws an error: module not found: javafx.controls. Windows 10. Java and JavaFX versions is 11.0.1

Again: I DID ADD the line --add-modules javafx.controls

Oksana Oryekhovska
  • 371
  • 1
  • 2
  • 7
  • Possible duplicate of [Module javafx.controls not found in Java 9](https://stackoverflow.com/questions/46349773/module-javafx-controls-not-found-in-java-9) – amin saffar Jan 05 '19 at 22:09
  • 3
    Downloading and unpacking the JavaFX files (https://gluonhq.com/products/javafx/) worked for me. I have the following VM options: `--add-exports=javafx.base/com.sun.javafx.reflect=ALL-UNNAMED --add-exports=javafx.graphics/com.sun.javafx.util=ALL-UNNAMED --module-path /opt/javafx-sdk-11.0.2/lib/ --add-modules=javafx.controls,javafx.media,javafx.fxml` –  Jul 11 '19 at 19:44

12 Answers12

10

I had to also include the 'lib' directory: --module-path %PATH_TO_FX%;%PATH_TO_FX%\lib to make it compile.

Arilou
  • 99
  • 1
  • 5
4

Make sure you don't have spaces in your list of modules:

--module-path PATH_TO_JAVAFX/lib --add-modules javafx.swing,javafx.graphics,javafx.fxml,javafx.media,javafx.controls
Nikolay Frick
  • 2,145
  • 22
  • 17
3

I'm using gradle and below build.gralde works fine for me (org.beryx.jlink is optional to build standalone distribution).

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
    id "org.beryx.jlink" version "2.3.0"  //optional for jlink
}

repositories {
    mavenCentral()
}

javafx {
    modules = [ 'javafx.controls' ]
}

mainClassName = 'gui.Main'
Leon
  • 3,124
  • 31
  • 36
3

The problem was solved in 3 steps.

  1. Use %PATH_TO_FX% instead of $PATH_TO_FX in the command line.

  2. Recreate the variables (both system and user) PATH_TO_FX enclosing its value in quotation marks. As the directory "C:\Program Files\Java\javafx-sdk-11.0.1\" contains a space, it caused an error "invalid flag".

  3. Rebooting the computer to update the variables.

Oksana Oryekhovska
  • 371
  • 1
  • 2
  • 7
2

Since I use Maven, I had to modify the pom.xml file to include the dependency.
pom.xml:

<dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>11.0.2</version>
        </dependency>
</dependencies>

You may need to modify the version numbers according to what version you have installed.

Pete F
  • 21
  • 1
  • 4
1

As JavaFX isn't included in "default" JDK's I found the easiest fix for this kind of issues is to use the LibericaJDK (11+) from Bellsoftware which has the JavaFX included again to make it easier to develop and run JavaFX applications.

https://bell-sw.com/pages/java-11.0.4/

Frank
  • 5,741
  • 4
  • 28
  • 49
0

helloFX.bat @echo off cls set PATH_TO_FX="C:\Program Files\Java\javafx-sdk-11.0.2\lib" C: cd \development\java\javaFX

rem javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml HelloFX.java javac --module-path %PATH_TO_FX% --add-modules javafx.controls HelloFX.java

java --module-path %PATH_TO_FX% --add-modules javafx.controls HelloFX

pause

Ion Stefanache
  • 147
  • 1
  • 4
0

When running the command

javac --module-path $PATH_TO_FX --add-modules javafx.controls HelloFX.java

instead of writing the $PATH_TO_FX or %PATH% write the path itself that is

javac --module-path C:\javafx-sdk-11.0.2\lib --add-modules javafx.controls HelloFX.java

Also try to make sure your directory doesn't have any spaces. It gave me problems, it may work for you for a directory with spaces

r_goyal
  • 1,117
  • 1
  • 11
  • 20
Seiden
  • 19
  • 2
0

My solution was little different from above:
javac --module-path C:\other_program\javafx-sdk-17.0.1\lib\ --add-modules javafx.controls HelloFX.java
I had to put lib in the path, and not C:\other_program\javafx-sdk-17.0.1\ together like Arilou suggestion.

To execute the compiled file, "HelloFX.class", you may include the lib path as well:
java --module-path C:\other_program\javafx-sdk-17.0.1\lib\ --add-modules javafx.controls HelloFX

Runtime environemnt:
  OS: Windows 10
  PowerShell
  Java 15.0.1
  JavaFX 17.01

Cloud Cho
  • 1,594
  • 19
  • 22
0

I am using maven. I had to add this dependency.

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>19-ea+2</version>
    </dependency>
Anders B
  • 3,343
  • 1
  • 26
  • 17
0

Follow these steps:

  1. Download and install the JavaFX SDK from the official OpenJFX website: https://openjfx.io/

  2. In IntelliJ, open your project and go to File > Project Structure.

  3. Select "Modules" from the left-hand side and then click the "+" button to add a new module.

  4. Select "JARs or directories" and browse to the "lib" folder in your JavaFX SDK installation directory.

  5. Select all the JAR files in the "lib" folder and click "OK".

  6. IntelliJ will now add the JAR files to your project's classpath.

This should do the job....This is for IntelliJ but this works for other IDEs as well or at least in a similar way..

Joe
  • 1
  • 1
-1

If you are using Netbeans, its a must to uncheck "Compile on Save", unless you get this error.

https://github.com/openjfx/openjfx-docs/issues/91#issuecomment-537354974

If you want a screenshot

https://github.com/openjfx/openjfx-docs/issues/120#issuecomment-692805305

Kishore
  • 1
  • 2