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

后端 未结 2 1252
我在风中等你
我在风中等你 2020-12-05 15: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 J

相关标签:
2条回答
  • 2020-12-05 16:07

    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.

    0 讨论(0)
  • 2020-12-05 16:08

    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.

    0 讨论(0)
提交回复
热议问题