Cannot execute JavaFX sample on macOS: No toolkit found

When trying to launch the JavaFX "Hello world" sample, the native libraries are not found -- or, they are rejected. The symptom is a "No toolkit found" exception. There seems to be some specificity to macOS, as signing is mentioned at some point.

What I did (on macOS 10.14.6):

  1. Installed Java 12 from https://adoptopenjdk.net
  2. Downloaded the JavaFX 12 SDK for macOS from http://gluonhq.com/download/javafx-12-0-2-sdk-mac. Unzipped it as $WRK_DIR/javafx-sdk-12.0.2
  3. Downloaded the JavaFX "hello world" sample from https://github.com/openjfx/samples. Unzipped it as $WRK_DIR/samples-master

Then I followed the instructions of the Getting Started (here or here). I chose the simplest path: no IDE, no build system like Maven, non-modular app, nothing but plain CLI.

$ WRK_DIR=$(pwd)
$ ls -lF
total 0
drwxr-xr-x@ 4 bruno  staff  128 19 jul 16:25 javafx-sdk-12.0.2/
drwxr-xr-x@ 8 bruno  staff  256 31 jul 10:18 samples-master/
$ PATH_TO_FX=$WRK_DIR/javafx-sdk-12.0.2/lib
$ ls $PATH_TO_FX 
javafx-swt.jar      javafx.properties   libglib-lite.dylib  libjfxwebkit.dylib
javafx.base.jar     javafx.swing.jar    libgstreamer-lite.dylib libprism_common.dylib
javafx.controls.jar javafx.web.jar      libjavafx_font.dylib    libprism_es2.dylib
javafx.fxml.jar     libdecora_sse.dylib libjavafx_iio.dylib libprism_sw.dylib
javafx.graphics.jar libfxplugins.dylib  libjfxmedia.dylib   src.zip
javafx.media.jar    libglass.dylib      libjfxmedia_avf.dylib
$ javac -version
javac 12.0.2
$ cd samples-master/CommandLine/Non-modular/CLI/hellofx
$ javac --module-path $PATH_TO_FX --add-modules=javafx.controls -d out $(find src -name "*.java")
$ find . -type f
./out/hellofx/HelloFX.class
./out/hellofx/Launcher.class
./src/hellofx/HelloFX.java
./src/hellofx/Launcher.java

Up until trying to launch:

$ java -version
openjdk version "12.0.2" 2019-07-16
OpenJDK Runtime Environment AdoptOpenJDK (build 12.0.2+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 12.0.2+10, mixed mode)
$ java --module-path $PATH_TO_FX --add-modules=javafx.controls -cp out hellofx.HelloFX
Graphics Device initialization failed for :  es2, sw
Error initializing QuantumRenderer: no suitable pipeline found
   <stack trimmed>
Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
   <stack trimmed>
Exception in thread "main" java.lang.reflect.InvocationTargetException
   <stack trimmed>
Caused by: java.lang.RuntimeException: No toolkit found
   <stack trimmed>

The research I did led me to think that the native libraries are not found. So I tried to add $PATH_TO_FX to LD_LIBRARY_PATH and DY LD_LIBRARY_PATH with no better result. Following this comment I added -Dprism.verbose=true to the command line:

$ java -Dprism.verbose=true --module-path $PATH_TO_FX --add-modules=javafx.controls -cp out hellofx.HelloFX
Prism pipeline init order: es2 sw 
Using Double Precision Marlin Rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.es2.ES2Pipeline
Loading ES2 native library ... prism_es2
GraphicsPipeline.createPipeline failed for com.sun.prism.es2.ES2Pipeline
java.lang.UnsatisfiedLinkError: no prism_es2 in java.library.path: [/Users/bruno/Library/Java/Extensions, /Library/Java/Extensions, /Network/Library/Java/Extensions, /System/Library/Java/Extensions, /usr/lib/java, .]
   <trimmed the rest of errors that are essentially the above>

So indeed there is a problem of (at least) libprism_es2.dylib not being found. I then tried to add $PATH_TO_FX to java.library.path:

$ java -Dprism.verbose=true -Djava.library.path=$PATH_TO_FX --module-path $PATH_TO_FX --add-modules=javafx.controls -cp out hellofx.HelloFX
Prism pipeline init order: es2 sw 
Using Double Precision Marlin Rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.es2.ES2Pipeline
Loading ES2 native library ... prism_es2
GraphicsPipeline.createPipeline failed for com.sun.prism.es2.ES2Pipeline
java.lang.UnsatisfiedLinkError: /Users/bruno/try/javafx-sdk-12.0.2/lib/libprism_es2.dylib: dlopen(/Users/bruno/try/javafx-sdk-12.0.2/lib/libprism_es2.dylib, 1): no suitable image found.  Did find:
    /Users/bruno/try/javafx-sdk-12.0.2/lib/libprism_es2.dylib: code signature in (/Users/bruno/try/javafx-sdk-12.0.2/lib/libprism_es2.dylib) not valid for use in process using Library Validation: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.
   <...>

Now the library is found, but is not considered valid.