Running javafx sample on JDK 11 with OpenJFX 11 JMODS on Module Path

25,348

I believe there is an explanation for the error you are facing: jmods can't be used at run time.

This is explained here: http://openjdk.java.net/jeps/261#Packaging:-JMOD-files:

JMOD files can be used at compile time and link time, but not at run time. To support them at run time would require, in general, that we be prepared to extract and link native-code libraries on-the-fly.

and credit goes to this answer.

So I've done some simple module hellofx:

module hellofx {
    requires javafx.controls;

    exports hellofx;
}

with the HelloFX sample from here and downloaded the jmods for JavaFX 11 for my platform from here. I've also downloaded the JavaFX 11 SDK (jars) from the same location.

Compile time

At compile time, we can do, with jmods:

javac -p /path-to/javafx-jmods-11/ -d mods/hellofx $(find src/hellofx -name "*.java")

or with SDK:

javac -p /path-to/javafx-sdk-11/lib -d mods/hellofx $(find src/hellofx -name "*.java")    

In both cases, the result is exactly the same, as expected: Native libraries are not required during compile time.

Run time

Now we want to run our little module.

With jmods, as stated by the OP, running:

java -p /path-to/javafx-jmods-11/:mods -m hellofx/hellofx.HelloFX   

fails with:

Error occurred during initialization of boot layer
  java.lang.module.FindException: Module javafx.controls not found, required by hellofx

But using the SDK, works:

java -p /path-to/javafx-sdk-11/lib/:mods -m hellofx/hellofx.HelloFX

Link time

As stated by the JEP-261, jmods work as well at link time, so we can use the jlink tool in between compile time and run time.

You can use the jlink tool to assemble and optimize a set of modules and their dependencies into a custom runtime image. (source)

So let's do:

jlink -p /path-to/javafx-jmods-11/:mods --add-modules=hellofx --output links

that will generate a folder with 90.7 MB (on my Mac). Note that the lib folder contains all the required native libraries from Java 11 and from JavaFX 11, as well as a 70.5 MB file named modules.

Run time (2)

And we can finally do:

links/bin/java -m hellofx/hellofx.HelloFX

And that will work.

In conclusion, if we want to use only jmods for compiling and running our modules, we need to give an extra step with jlink. Otherwise, for runtime we'll need the JavaFX SDK.

Share:
25,348

Related videos on Youtube

MohamedSanaulla
Author by

MohamedSanaulla

I am software developer with 9+ years of experience. I specialize in the following stack: Front End - Bootstrap, jQuery, Mustache.js, Vuejs Back End - Spring MVC, Spring Boot, Thymeleaf, MyBatis Datastores - Oracle, MySQL, Elasticsearch

Updated on February 08, 2020

Comments

  • MohamedSanaulla
    MohamedSanaulla about 4 years

    I have downloaded the JavaFX Jmod files from OpenJFX project and placed them in the directory G:\openjfx\javafx-jmods-11. I am using OpenJDK 11 which has no JavaFX jmod in JAVA_HOME/jmods i.e it doesn't come with JavaFX distribution.

    Module info file:

    module gui{
        requires javafx.graphics;
        requires javafx.controls;
    
        exports com.test;
    }
    

    I compile with following:

    javac -p G:\openjfx\javafx-jmods-11 -d mods --module-source-path src 
        src\gui\com\test\*.java src\gui\module-info.java
    

    Compilation succeeds. But I am unable to run the compiled code using the below command:

    java -p G:\openjfx\javafx-jmods-11;mods -m gui/com.test.CreateGuiDemo
    

    But I get the below error:

    Error occurred during initialization of boot layer
    java.lang.module.FindException: Module javafx.graphics not found, required by gui
    
    • kleopatra
      kleopatra over 5 years
      not entirely certain, but I think you have to specify both the module-path (which you do) and the modules to add both on compile and runtime path: --add-modules javafx.controls
    • Naman
      Naman over 5 years
      Would require a bit of clarification, if you could add details to the question, 1. what is dir1? 2. What is mods? 3. Did you get a chance to try out the same with the SDK as well? ... There definitely seem to be some noise related to resolving the module at runtime, something that might actually be useful could be the effective command shared by Stephan here.
    • mipa
      mipa over 5 years
      @kleopatra As far as I understand it, this is only necessary if you do not have a module-info.java file. So, in this case it should not be necessary.
    • mipa
      mipa over 5 years
      @Mohamed I'd also follow the advice of nullpointer and use the SDK with the jars instead of the jmods.
    • kleopatra
      kleopatra over 5 years
      @mipa thanks tor the heads-up - still in the groping phase ;) and with my mind inside the bug report that nullpointer referenced ...
    • MohamedSanaulla
      MohamedSanaulla over 5 years
      @nullpointer sorry I couldn't find time to try it out. I will check it out in a day or two. As my sample app is a modular app so I didn't need to use the --add-modules option. Compilation is fine, but on run time the command line option --module-path isn't taking more than one path element
    • MohamedSanaulla
      MohamedSanaulla over 5 years
      I will try with the SDK jar and get back ...
    • MohamedSanaulla
      MohamedSanaulla over 5 years
      I tried with the SDK jar and it works. I put the jars on the modulepath. I even tried with jmods of OpenJFX 11, but no success.
  • MohamedSanaulla
    MohamedSanaulla over 5 years
    Thanks a lot for the detailed answer. Yes, at runtime SDK worked for me. Let me try the jLink approach as well. Very informative.
  • MohamedSanaulla
    MohamedSanaulla about 5 years
    I wanted to run JavaFX on JDK 11. So switching to JDK 8 was not an option.
  • Searjasub
    Searjasub about 5 years
    I edited the answer. Try it and let me know if it worked.
  • NobodyMan
    NobodyMan almost 5 years
    Why would you ever want to use the jmod files over the SDK?