Module not found: javafx.controls

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

2

11 Answers

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

1

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 

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' 

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.

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.

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

1

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

1

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.

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

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> 

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

If you want a screenshot

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like