JavaFX module in JRE

Discussion in 'App Development' started by deanstreet, Jun 12, 2021.

  1. I am just made aware that there is this new thing called a module in Java from Java9, and that JavaFX is isolated/unbundled from Java11, so JavaFX needs to be separately downloaded from OpenJFX. I have no idea what's the difference between the SDK and jmods (Java modules) downloads, and no I am a beginner and not using any Maven/Gradle builds.

    To compile a file.java having some module imports, it seems I must explicitly provide the module path (instead of setting a fixed path like classpath)

    Code:
    javac --module-path THE_PATH_TO_JAVAFX_LIB  --add-modules javafx.controls the_file.java
    
    I would prefer using the JavaFX module just like a good-old package and to have a classpath automatically locate the imported classes. In Windows, I do
    Code:
    set JAVAFX="Program Files\path\javafx-sdk-11.0.1\lib"
    
    But I still have to do a long and error-prone
    Code:
    javac --module-path %JAVAFX%  --add-modules javafx.controls the_file.java
    So I decided to instead shorten the pain
    Code:
    set JAVAFX=--module-path "Program Files\path\javafx-sdk-11.0.1\lib"  --add-modules javafx.controls
    javac %JAVAFX% the_file.java
    
    Up to this point, fine although I still don't understand Java modules. But I don't understand why I also need to provide %JAVAFX% to run the compiled file.

    Code:
    java the_file //java.lang.NoClassDefFoundError
    java %JAVAFX% the_file //this is fine
    This suggests that JavaFX isn't included in JRE. So we need to have both JRE and JavaFX runtime to use Java applications? How are users expected to have JavaFX runtime on top of JRE? Or is it the developer's job to include JavaFX runtime into a jar, so anyone can open and have JavaFX working? That is, how to just have JVM run any classes (having javafx or not)?
     
    Last edited: Jun 12, 2021
  2. Millionaire

    Millionaire

    Use jlink to build the runtime for your application so it cotains all the modules the app needs.

    Alternatively ship the whole jre with extra javafx modules included.

    Just google jlink
     
    deanstreet likes this.
  3. So nowadays, if I (as an end-user) use appX and appY (both having GUI), the GUI modules are included in both individually and separately, instead of me having all JavaFX modules in my JVM ?
     
  4. Millionaire

    Millionaire

    As an end user you normally get the whole run time, jre/modules/app jars/third party jars all bundled into a runtime installer. eg how IB TWS is shipped to end users.

    There are also somtimes incompatibilities and bugs in some particular JRE/JavaFx versions. So you ship the jre and javafx modules that are tested with your app and known to work well.
     
    deanstreet likes this.
  5. Kind of understandable from the supplier's perspective (packing everything into one to ensure compatibility), but seems like a waste of bytes having different JRE individually packaged whilst I have a reasonably up-to-date JRE (Java11). So my JRE nowadays has no GUI functionality? That is quite a paradigm shift, and I wonder why it hasn't been much talked about in the news (jk).
     
  6. Millionaire

    Millionaire

    Still has Java Swing.
    JavaFX wasnt that popular, had only been around for 5 years when it was removed the jdk.
    Swing has been around for 23 years and is still part of the JDK.

    Its not a huge issue just ship the needed runtime with your application to end users.
     
    dholliday likes this.
  7. But you want to play around and trying different things back and forth when writing. I don't know how to try JavaFX using JShell, VS Code, etc, without first compiling and java --module-path %JAVAFX% --add-modules javafx.controls hello_world
     
  8. Millionaire

    Millionaire

    Use IntelliJ or Eclipse as your IDE for Java development.
    I dont know any Java dev who uses anything else.

    Also consider using Gradle or Maven.
     
  9. It seems using VS Code for JavaFX or anything dealing with GUI can be difficult and complicated. I came across this teaching IDE BlueJ which is very basic but easy to use and try things out for beginners with JavaFX support.
     
  10. dholliday

    dholliday

    I agree with Millionaire, if you are writing a desktop app, use Swing. Swing was developed by some of the same engineers that developed the UI library for Next Step, Steve Jobs company Next. Swing is today at the top of the heap for desktop UI libraries.
     
    #10     Jun 13, 2021