Bundle JavaFX app with openjdk 11 + runtime

前端 未结 5 1495
生来不讨喜
生来不讨喜 2020-12-16 01:52

I\'ve created a small HelloWorld Java app that relies on OpenJDK 11 and JavaFX. The app is packaged in a jar file which can only be run if I have installed Java 11 and JavaF

5条回答
  •  青春惊慌失措
    2020-12-16 02:25

    Native Libraries

    A challenge I encountered was to inform JavaFX about it's own native libraries (.dll, .dylib, .so, etc). Fortunately, getting the dylibs loaded is as simple as setting the java.library.path using System.setProperty(...).

    Historically, setting this variable is argued/perceived as pointless in Java as it's too late for the classloader (inferior to -Djava.library.path) and forcing it using reflection is a forbidden security violation since Java 10... fortunately, JavaFX actually honors this variable naturally without any violations or hacks and will pick it up after it's set.

    // Detect the path to the currently running jar
    String jarPath = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getCanonicalPath();
    
    // Fix characters that get URL encoded when calling getPath()
    jarPath = URLDecoder.decode(jarPath, "UTF-8");
    
    String parentFolder = new File(jarPath).getParent();
    
    // If libglass.dylib is next to the jar in a folder called "/bin"
    System.setProperty("java.library.path",  parentFolder + "/bin");
    
    // ... then make any javafx calls
    

    Java Libraries

    Naturally, the .jar files need to be accessible too. I do this the same as I would any java bundle by zipping them into the distribution (making a single, large .jar file)

    These .jar files should be consistent with all JavaFX 11 distributions and should be bundled accordingly.

    javafx-swt.jar
    javafx.base.jar
    javafx.controls.jar
    javafx.fxml.jar
    javafx.graphics.jar
    javafx.media.jar
    javafx.swing.jar
    javafx.web.jar
    

    Java 8 Compatibility

    Initial tests against Java 8 using the above technique are positive. For now, I'm using Java version detection (not included in the above example) and ONLY setting java.library.path for Java 11 or higher. Java 8 is EOL for personal use Dec 2019 (EOL for commercial use Jan 2019) so it is important to offer compatibility as clients migrate from one LTS release to another.

提交回复
热议问题