Skip to content

Instantly share code, notes, and snippets.

@TheFern2
Created September 12, 2019 01:48
Show Gist options
  • Save TheFern2/029adae80464fdfd3f8a5c4e56c1f3c6 to your computer and use it in GitHub Desktop.
Save TheFern2/029adae80464fdfd3f8a5c4e56c1f3c6 to your computer and use it in GitHub Desktop.
JavaFX Application Deployment

Install Java JDK 11-13

I will be using Java 11 since is the LTS release.

Check current version in path:

java -version

JavaFX 13 and IntelliJ

This section explains how to use Java 13 and JavaFX 13 from IntelliJ. Version IntelliJ IDEA 2019.1 was used for the following screenshots.

Download an appropriate JDK 13 for your operating system. Make sure JAVA_HOME is properly set to the Java 13installation directory.

You can create a JavaFX 13 modular or non-modular project and use the IDE tools, Maven or Gradle build tools.

Non-modular projects

IDE

Follow these steps to create a JavaFX non-modular project and use the IDE tools to build it and run it. Alternatively, you can download a similar project from here.

Download the appropriate JavaFX SDK for your operating system and unzip it to a desired location, for instance/Users/your-user/Downloads/javafx-sdk-13.

1. Create a JavaFX project

Create a JavaFX projectProvide a name to the project, like HelloFX, and a location. When the project opens, the JavaFX classes are not recognized.Missing JavaFX classes

2. Set JDK 13

Go to File -> Project Structure -> Project, and set the project SDK to 13. You can also set the language level to 13.Set JDK 11

3. Create a library

Go to File -> Project Structure -> Libraries and add the JavaFX 13 SDK as a library to the project. Point to the lib folder of the JavaFX SDK.Create LibraryOnce the library is applied, the JavaFX classes will be recognized by the IDE.JavaFX classes are recognized

Warning: If you run now the project it will compile but you will get this error:


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

This error is shown since the Java 13 launcher checks if the main class extendsjavafx.application.Application. If that is the case, it is required to have the javafx.graphics module on the module-path.

4. Add VM options

To solve the issue, click on Run -> Edit Configurations... and add these VM options:


--module-path "\path\to\javafx-sdk-13\lib" --add-modules javafx.controls,javafx.fxml

Note that the default project created by IntelliJ uses FXML, so javafx.fxml is required along with javafx.controls. If your project uses other modules, you will need to add them as well.VM optionsClick apply and close the dialog.

Alternatively, you can define a global variable that can be used in future projects. Go to Preferences (File -> Settings) -> Appearance & Behavior -> Path Variables, and define the name of the variable as PATH_TO_FX, and browse to the lib folder of the JavaFX SDK to set its value, and click apply.Path Variable

Then you can refer to this global variable when setting the VM options as:


--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml

Path Variable in VM options

5. Run the project

Click Run -> Run... to run the project, now it should work fine.

Maven

Follow these steps to create a JavaFX non-modular project and use the Maven tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a Maven project

Select File -> New -> Project -> Maven and enable Create from archetype. If the JavaFX archetype is not installed yet, select Add archetype... and set the groupId (org.openjfx), the artifactId (javafx-maven-archetypes), and the version (0.0.1), and press OK.JavaFX Archetype

Once installed, select the artifact.Create a Maven projectProvide the groupId, like org.openjfx, the artifactId, like hellofx.

Select the archetype artifactId, between javafx-archetype-fxml or javafx-archetype-simple, based on the use of FXML or not in your project.Select archetypeYou can create a property for the javafx-version as well, and set it to 13.

Finally, provide a name to the project, like HelloFX and a location for the project.

2. Verify the project

You can find the generated pom file here.

Verify it includes the javafx.controls and javafx.fxml dependencies, and includes the javafx-maven-plugin.Generated pomFor a non-modular project, you can remove the module-info.java file.

Import the Maven changes. The JavaFX classes will be recognized. Notice also that Maven manages the required dependencies: it will add javafx.base and javafx.graphics that are required by javafx.controls and javafx.fxml, but most important, it will add the required classifier based on your platform, downloading the specific platform jars.Platform dependenciesAs for any other maven dependencies, these jars can be found in the local .m2 repository.

3. Run the project

You can open the Maven Projects window and click on HelloFX -> Plugins -> compiler -> compiler:compile to compile the project, and click on HelloFX -> Plugins -> javafx -> javafx:run to execute the project.Run project

Note: In case JAVA_HOME is not set to 13, running from the Maven Projects window might fail. To avoid it, you can add the correct java command to the javafx-maven-plugin: /path/to/jdk-12/bin/java.

The project can be also run from a terminal. Make sure JAVA_HOME is set to 13, and run mvn clean javafx:run.

Gradle

Follow these steps to create a JavaFX non-modular project and use the Gradle tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a Gradle project

Create a Gradle project with Java.Create a Gradle projectProvide the groupId, like org.openjfx, the artifactId, like hellofx. Select the Gradle JVM based on the project JDK 13. Then provide a name to the project, like HelloFX and a location for the project. When the project opens, add a package org.openjfx and an empty MainApp class.Open project

2. Modify the build

Edit the build.gradle file and replace it with this build file, setting the mainClassName accordingly to org.openjfx.MainApp.

Similar to Maven, we can declare the required JavaFX modules in the build.gradle file. However, for Gradle we need to apply the JavaFX gradle plugin:


javafx {
    version = "13"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

Synchronize the project and you will get the JavaFX dependencies.Update the buildAs for any other Gradle dependencies, these jars can be found in the local .gradle repository.

3. Add the source code

Based on this MainApp class, add its content to the project main class. Then add the controller and the FXML and and the css files. Note that the JavaFX classes are recognized by the IDE.HelloFX

4. Run the project

You can open the Gradle window and click on hellofx -> Tasks -> build -> build to build the project, and on hellofx -> Tasks -> application -> run to execute the project.Run project

You can also open a terminal and run:


gradlew run

Modular projects

Download the appropriate JavaFX jmods for your operating system and unzip it to a desired location, for instance/Users/your-user/Downloads/javafx-jmods-13.

IDE

Follow these steps to create a JavaFX modular project and use the IDE tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a JavaFX project

Provide a name to the project, like HelloFX, and a location. When the project opens, rename the hellofxpackage to org.openjfx.

2. Set JDK 13 and add JavaFX 13

Go to File -> Project Structure -> Project, and set the project SDK to 13. You can also set the language level to 13 and change the default compiler output directory out to mods.mods output

Go to File -> Project Structure -> Libraries and add the JavaFX 13 SDK as a library to the project. Point to the lib folder of the JavaFX SDK.

3. Add the module-info class

Add the module-info class, including the required modules javafx.controls and javafx.fxml. Since FXML uses reflection to access the controller in the module, this has to be opened to javafx.fxml. Finally, export the package org.openjfx.module-info

4. Add the source code

Based on this MainApp class, add its content to the project main class. Then add the controller and the FXML and and the css files.Source code

5. Add VM options

Click on Run -> Edit Configurations... and add these VM options:


--module-path "%PATH_TO_FX%;mods\production"

VM optionsClick apply and close the dialog.

6. Run the project

Click Run -> Run... to run the project.

7. Create a custom runtime image

To create a runtime image, run the following commands:


set PATH_TO_FX_MODS="path\to\javafx-jmods-13"
jlink --module-path "%PATH_TO_FX_MODS%;mods\production" --add-modules hellofx --output jre
jre\bin\java -m hellofx/org.openjfx.MainApp

Maven

Follow these steps to create a JavaFX modular project and use the Maven tools to build it and run it. Alternatively, you can download a similar project from here.

Select File -> New -> Project -> Maven and enable Create from archetype. If the JavaFX archetype is not installed yet, select Add archetype... and set the groupId (org.openjfx), the artifactId (javafx-maven-archetypes), and the version (0.0.1), and press OK.JavaFX Archetype

Once installed, select the artifact.Create a Maven projectProvide the groupId, like org.openjfx, the artifactId, like hellofx.

Select the archetype artifactId, between javafx-archetype-fxml or javafx-archetype-simple, based on the use of FXML or not in your project.Select archetypeYou can create a property for the javafx-version as well, and set it to 13.

Finally, provide a name to the project, like HelloFX and a location for the project.

2. Verify the project

You can find the generated pom file here.

Verify it includes the javafx.controls and javafx.fxml dependencies, and includes the javafx-maven-plugin.Generated pom

Open the module-info class, that includes the required modules javafx.controls and javafx.fxml. Since FXML uses reflection to access the controller in the module, it has been opened to javafx.fxml.module-info

3. Run the project

You can open the Maven Projects window and click on HelloFX -> Plugins -> compiler -> compiler:compile to compile the project, and click on HelloFX -> Plugins -> javafx -> javafx:run to execute the project.

Note: In case JAVA_HOME is not set to 13, running from the Maven Projects window might fail. To avoid it, you can add the correct java command to the javafx-maven-plugin: /path/to/jdk-12/bin/java.

The project can be also run from a terminal. Make sure JAVA_HOME is set to 13, and run mvn clean compile package javafx:run.

4. Create a custom runtime image

To create a custom runtime, using the JavaFX Maven plugin, you can click on HelloFX -> Plugins -> javafx -> javafx:jlink, or from a terminal with JAVA_HOME set to 13 you can run:


mvn clean javafx:jlink

Note the plugin allows the usual options as the jlink command, as well as creating a launcher or a zip with the custom image.

And after the image is built, you can run it from command line:


target\hellofx\bin\launcher

Gradle

Follow these steps to create a JavaFX modular project and use the Gradle tools to build it and run it. Alternatively, you can download a similar project from here.

1. Create a Gradle project

Create a Gradle project with Java. Provide the groupId, like org.openjfx, the artifactId, like hellofx. Select the Gradle JVM based on the project JDK 13. Then provide a name to the project, like HelloFX and a location for the project. When the project opens, add a package org.openjfx and an empty MainApp class.

2. Modify the build

Edit the build.gradle file and replace it with this build file, setting the mainClassName accordingly to org.openjfx.MainApp.

Note the use of the org.openjfx.javafxplugin plugin, that removes the necessity of adding the JavaFX dependencies and setting the module-path for the compile and run task for them.Update the build

3. Add the module-info class

Add the module-info class, including the required modules javafx.controls and javafx.fxml. Since FXML uses reflection to access the controller in the module, this has to be opened to javafx.fxml. Finally, export the package org.openjfx.module-info

4. Add the source code

Based on this MainApp class, add its content to the project main class. Then add the controller and the FXML and and the css files.

5. Run the project

You can open the Gradle window and click on hellofx->Tasks->build->build to build the project, and on hellofx->Tasks->application->run to execute the project. You can also open a terminal and run:


gradlew run

6. Create a custom runtime image

To create a custom runtime, you can use the org.beryx.jlink plugin. It can be easily combined with the JavaFX Gradle plugin:


plugins {
  id 'application'
  id 'org.openjfx.javafxplugin' version '0.0.8'
  id 'org.beryx.jlink' version '2.12.0'
}

javafx {
    version = "13"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

jlink {
    launcher {
        name = 'hellofx'
    }
}

to generate the custom image. Run hellofx -> Tasks -> build -> jlink task to create the image.

To run the image, type on a terminal:


build\image\bin\hellofx
@TheFern2
Copy link
Author

On a second thought my application doesn't need to be cross platform and therefore I don't need java, I will use WPF with Visual Studio and that leaves any configuration out of the picture and I can concentrate on the app itself.

@durfejs
Copy link

durfejs commented Jul 11, 2022

I tried the modular IDE - IntelliJ and did not work. I had to change

--module-path "%PATH_TO_FX%;mods\production"
for
--module-path ${PATH_TO_FX};mods/production

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment