Reputation: 21
I've created small project using javafx. In Intellij idea it works just fine. But when I build it and tried to run by
java -jar d:\program.jar
in window's command promt it gave me Error: JavaFX runtime components are missing, and are required to run this application.
I've also tried
java -jar d:\program.jar --module-path d:\javafx\lib --add-modules javafx.controls,javafx.fxml
but it didn't work too
Any ideas?
Upvotes: 0
Views: 192
Reputation: 187
Since Java 11, the JavaFX runtime has been removed from the JDK and it is now its own separate module. This means you need to bundle the requirements for JavaFX in your JAR file (fat jar). Do note that the JavaFX requirements are platform-dependent, which means you'll likely need to create separate JAR files for each platform you're targeting. For Gradle, this will look something like this:
...
def currentOS = DefaultNativePlatform.currentOperatingSystem;
def platform
if (currentOS.isWindows()) {
platform = 'win'
} else if (currentOS.isLinux()) {
platform = 'linux'
} else if (currentOS.isMacOsX()) {
platform = 'mac'
}
dependencies {
implementation "org.openjfx:javafx-base:15.0.1:${platform}"
implementation "org.openjfx:javafx-controls:15.0.1:${platform}"
implementation "org.openjfx:javafx-graphics:15.0.1:${platform}"
implementation "org.openjfx:javafx-fxml:15.0.1:${platform}"
...
}
On the other hand, if you're working with a modular project (Java 9+), you will need to add the required modules when executing the application. For example, in order to run the JAR file (make sure you're using Java 9+, you can check this by running java --version
):
java --module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.graphics,javafx.fxml,javafx.web -jar MyApp.jar
Where the modules that were added using --add-modules
argument are the required modules for your specific needs and the ${PATH_TO_FX}
variable is the path where the JavaFX runtime can be found.
Do note that this method requires your clients to have the JavaFX runtime 'installed' (and a JRE) on their systems and is generally speaking no longer the recommended way to distribute your Java application.
If you're using Maven or Gradle, there are excellent plugins available to create jlink'ed images that contain your modular project and all dependencies, including a JRE that is optimized for your application and the JavaFX modules. This is, generally speaking, the recommended way to distribute Java 9+ applications to your clients. The plugins in question are:
https://badass-jlink-plugin.beryx.org/releases/latest/
https://maven.apache.org/plugins/maven-jlink-plugin/
This does require that you create a module-info.java
file:
https://openjdk.java.net/projects/jigsaw/quick-start
Upvotes: 1