October 21, 2018
With Oracle ending free public updates of Java in January 2019, you might be looking for an alternative. There is one called the OpenJDK. However for JavaFX apps, this is not a drop-in replacement. The different distros of OpenJDK do not ship with JavaFX as they do with the Oracle JDK. This article shows how you can build your own copy of JavaFX to supplement the OpenJDK.
These procedures update the official documentation and describe how to build JavaFX 8 with Microsoft Visual Studio 2017 (the current version as of October 2018). JavaFX 8 is not the latest version of JavaFX (it is 11 at the time of this post). Since there are many breaking changes in JavaFX 9, 10, and 11, I expect JavaFX 8 to be in the field for the next few years.
The Windows environment requires Cygwin which provides a command shell. I downloaded my copy from here.https://www.cygwin.com/
OpenJFX uses Gradle to compile the Java and native code and to package the distro. I verified 4.8.1. You'll get warnings if you try to use something later like the current version 4.10.2. Earlier versions won't work.https://gradle.org/releases/
The OpenJDK distro that I'm working with is from Azul.https://www.azul.com/downloads/zulu/
This is another OpenJDK distro from AdoptOpenJDK. I haven't built OpenJFX with it, but I have installed the OpenJFX Java and native libraries in an AdoptOpenJDK distro and run without an issue.https://adoptopenjdk.net/
Because the Oracle JDK includes JavaFX, you won't be able to build against it. The Oracle JDK will need to be removed from your JAVA_HOME and PATH environment variables.
OpenJFX uses the Mercurial source code management system. You don't need to know much Mercurial to work with the OpenJFX source. You just need to clone and switch the tag to the version that you want. These procedures will build from the tip (like the HEAD in Git). Here is the command to clone the latest JavaFX 8 code.
$ hg clone http://hg.openjdk.java.net/openjfx/8u-dev/rt
Cygwin comes with Mercurial so you may not need a separate download.
This is not the latest JavaFX which is 11 as of October 2018.
JavaFX includes native code that must be compiled with a C++ compiler. For Windows, this is the cl.exe compiler packaged with Visual Studio. I've tested this with Visual Studio 2017 Professional. When you install 2017, be sure to check the "Desktop Development" feature. You can verify this by starting Visual Studio and going to Tools > Get Tools and Features. You should see the following item checked.
The following environment variables need to be set prior to running the build command. I've put these definitions in my .bash_profile.
export JAVA_HOME=D:/Java/zulu22.214.171.124-jdk8.0.181-win_x64 export GRADLE_HOME=C:/gradle/gradle-4.8.1 export PATH="/cygdrive/d/Java/zulu126.96.36.199-jdk8.0.181-win_x64/bin:/cygdrive/c/gradle/gradle-4.8.1/bin:$PATH" export MSVC_VER=14.15.26726
"Zulu" is the name of the Azul OpenJDK distro. MSVC_VER is a variable that needs to be set so that the build script can find the native compiler, cl.exe. If you have a later version of Visual Studio, you might need a different number. Here is the path and folder for mine. If you're running "Community" (which I haven't tested), swap "Community" for "Professional".
Once all the software and environment is set up, the entire project builds with a single command. Change into the root of the source and run
The artifacts will be in the build/sdk folder. If you're interested only in the jfxrt.jar -- needed to compile your JavaFX app -- that's found in build/sdk/rt/lib/ext.
The no-arg Gradle command builds the basic windowing system. However, it is missing the supporting native libraries for Media and WebKit. Building Media is important if your application uses classes like AudioClip. Your AudioClip references will compile, but you'll get a runtime error looking for unlinked libaries like glib_lite.
There are a few extra installation requirements in order for COMPILE_MEDIA to work.
The Windows 7 SDK setup may display a warning. The command should produce files in the following location: C:\Program Files\Microsoft SDKs\Windows\v7.1. I believe this path is hardcoded so avoid using a different disk.
To build with the Media native code run
An OpenJFX 8 build produces
To compile a JavaFX application, reference jfxrt.jar in your IDE or build tool. Adding jfxrt.jar as you would any other JAR or use the java.ext.dir property.
To run a JavaFX application, you need both the Java code -- jfxrt.jar -- and the native code. These instructions produced Windows native code in the form of .dll files. Some of these were produced by the build from C++ code such as glass.dll and prism_common.dll. Other DLLs were copied from the Windows system on which I'm building OpenJFX (for example, api-ms-win-core-console-l1-1-0.dll).
If you use the java.ext.dirs property or add the jfxrt.jar into your JRE's ext/ folder, you'll have to either specify the java.library.path property or keep the put the DLLs in the JRE bin/ folder. (There is a class NativeLibLoader which will attempt to resolve the native code position as ../../bin.)
Here's how I ran my HelloWorldFX program.
Carl2@MSI /cygdrive/d/git/openjfx-8-project/out/production/openjfx-8-project $ echo $JAVA_OPTS -Djava.ext.dirs=D:/hg/rt/build/sdk/rt/lib/ext Carl2@MSI /cygdrive/d/git/openjfx-8-project/out/production/openjfx-8-project $ java $JAVA_OPTS o8p.HelloWorldFX
Once the mailing list pointed me to the right Mercurial repo, the code built without issue. I did have to set the MSC_VER which is mentioned on a different page of build instructions (not the 8u). For my test, I ran a simple JavaFX Hello World. I'll be testing more rigorously throughout the coming months so look for the next articles on Linux and MacOS.
By Carl Walker
President and Principal Consultant of Bekwam, Inc