The javafx-maven-plugin
should be able to do what you are trying to do. However, it doesn't do it so far, so I've just filed these two issues: Options for javafx:run are incompatible with javafx:jlink and Missing link vm options parameter.
While this gets resolved and a new version is published, there is an easy (but manual) fix:
Compilation time
Before modifying the javafx-maven-plugin
, you need to allow your IDE to work with the private package. You can't do it from the module-info, but you can easily do that from the maven-compiler-plugin
using compilerArgs
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>--add-exports</arg>
<arg>javafx.graphics/com.sun.glass.ui=com.andrei</arg>
</compilerArgs>
</configuration>
</plugin>
Now in your code you can make use of that private package, and IntelliJ won't complain.
After running from the Maven window Lifecycle -> clean
, and Lifecycle -> compile
, something like this is allowed in the editor:
@Override
public void start(Stage stage) throws Exception {
...
stage.setScene(scene);
stage.show();
com.sun.glass.ui.Window.getWindows().forEach(System.out::println);
}
Runtime
However, if you do mvn clean compile javafx:run
, the code above will fail:
Caused by: java.lang.IllegalAccessError: class com.andrei.Main (in module com.andrei) cannot access class com.sun.glass.ui.Window (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.ui to module com.andrei.
As explained in the plugin readme, you cad add VM options that will be passed to the java
tool:
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<options>
<option>--add-opens</option>
<option>javafx.graphics/com.sun.glass.ui=com.andrei</option>
</options>
...
</configuration>
</plugin>
Now you can run: mvn clean compile javafx:run
, and that will work, and you will get information for the current stage printed out.
Runtime image
Finally, if you run: mvn clean compile javafx:jlink
, this will fail, because the content in <options>
is not recognized by jlink
(first issue filed), so you have to comment it out:
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<!--<options>-->
<!--<option>--add-opens</option>-->
<!--<option>javafx.graphics/com.sun.glass.ui=com.andrei</option>-->
<!--</options>-->
<launcher>launcher</launcher>
<mainClass>com.andrei/com.andrei.Main</mainClass>
...
</configuration>
</plugin>
Now mvn clean compile javafx:jlink
will work, but when running you will get the same error as above because the private package is not exported.
However, you can edit the launcher file under target/image/bin/launcher
:
#!/bin/sh
JLINK_VM_OPTIONS=
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m com.andrei/com.andrei.Main $@
As you can see, there is an empty JLINK_VM_OPTIONS
variable that can be filled with your vm options.
Until the second issue filed is solved, just modify that line:
#!/bin/sh
JLINK_VM_OPTIONS="--add-opens javafx.graphics/com.sun.glass.ui=com.andrei"
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m fx/org.openjfx.MainApp $@
save, and run: target/image/bin/launcher
, and it will work.