16

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
José Pereda
  • 39,900
  • 6
  • 84
  • 114
MohamedSanaulla
  • 5,802
  • 5
  • 25
  • 42
  • 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 – kleopatra Aug 14 '18 at 14:15
  • 1
    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](http://mail.openjdk.java.net/pipermail/openjfx-dev/2018-May/021819.html), something that might actually be useful could be [the effective command shared by Stephan here](https://bugs.eclipse.org/bugs/show_bug.cgi?id=534572#c19). – Naman Aug 14 '18 at 15:45
  • 2
    @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 Aug 15 '18 at 08:20
  • 1
    @Mohamed I'd also follow the advice of nullpointer and use the SDK with the jars instead of the jmods. – mipa Aug 15 '18 at 08:23
  • @mipa thanks tor the heads-up - still in the groping phase ;) and with my mind inside the bug report that nullpointer referenced ... – kleopatra Aug 15 '18 at 08:26
  • 1
    @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 Sep 09 '18 at 11:28
  • I will try with the SDK jar and get back ... – MohamedSanaulla Sep 09 '18 at 11:32
  • 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 Sep 25 '18 at 06:19

2 Answers2

26

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.

José Pereda
  • 39,900
  • 6
  • 84
  • 114
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.

Searjasub
  • 1
  • 3