Running javafx sample on JDK 11 with OpenJFX 11 JMODS on Module Path
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.
Related videos on Youtube
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, 2020Comments
-
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 inJAVA_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 over 5 yearsnot 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 over 5 yearsWould require a bit of clarification, if you could add details to the question, 1. what is
dir1
? 2. What ismods
? 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 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 over 5 years@Mohamed I'd also follow the advice of nullpointer and use the SDK with the jars instead of the jmods.
-
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 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 over 5 yearsI will try with the SDK jar and get back ...
-
MohamedSanaulla over 5 yearsI 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 over 5 yearsThanks a lot for the detailed answer. Yes, at runtime SDK worked for me. Let me try the jLink approach as well. Very informative.
-
MohamedSanaulla about 5 yearsI wanted to run JavaFX on JDK 11. So switching to JDK 8 was not an option.
-
Searjasub about 5 yearsI edited the answer. Try it and let me know if it worked.
-
NobodyMan almost 5 yearsWhy would you ever want to use the jmod files over the SDK?