Running javafx sample on JDK 11 with OpenJFX 11 JMODS on Module Path
Asked Answered
A

2

19

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
Aten answered 14/8, 2018 at 13:55 Comment(8)
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.controlsMaori
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.Sedgewick
@Maori 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.Tropho
@Mohamed I'd also follow the advice of nullpointer and use the SDK with the jars instead of the jmods.Tropho
@Tropho thanks tor the heads-up - still in the groping phase ;) and with my mind inside the bug report that nullpointer referenced ...Maori
@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 elementAten
I will try with the SDK jar and get back ...Aten
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.Aten
A
31

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.

Aho answered 5/10, 2018 at 21:25 Comment(2)
Thanks a lot for the detailed answer. Yes, at runtime SDK worked for me. Let me try the jLink approach as well. Very informative.Aten
Why would you ever want to use the jmod files over the SDK?Prady
B
0

If it is not automatically added it, try using this setup in the pom.xml. Be sure to change the "!!YOUR MAIN CLASSNAME HERE!!" towards the bottom of the code to the name of your class with the main method! If my class is Example.java I will want to put in there just Example.

 <dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>!!YOUR MAIN CLASSNAME HERE!!</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

JavaFX is not automatically added as a dependency with Java 11. That's why we need added manually.

Bedspread answered 7/2, 2019 at 6:51 Comment(2)
I wanted to run JavaFX on JDK 11. So switching to JDK 8 was not an option.Aten
I edited the answer. Try it and let me know if it worked.Bedspread

© 2022 - 2024 — McMap. All rights reserved.