posted on: 2019-03-14 06:14:08
New to java 9 is the module concept. OpenJFX is now distributed as a module.

To start with you need to download javafx. You only need the SDK for making and running programs, but if you want to use JLink to create a distributable package with the necessary jvm, you'll need the jmods package first.

Here is our "hello world" taken from here.

package hellofx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class HelloFX extends Application {

    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

Classic compilation and run, javac, jars, and .so's.

javac -cp javafx.controls.jar:javafx.base.jar:javafx.graphics.jar -d build *.java

Because of the new module structure we cannot launch our application without using modules and we need to create an auxiliary file, it has been discussed here.

Our helper class:


package hellofx;

public class Launcher{
    
    public static void main(String[] args){
        HelloFX.main(args);
    }
}

Then we can recompile, include some jars and include the directory containing .so's.

java -Djava.library.path=javafx-sdk-11.0.1/lib/ -cp javafx.controls.jar:javafx.base.jar:javafx.fxml.jar:javafx.graphics.jar:javafx.media.jar:javafx.swing.jar:javafx.web.jar:javafx-swt.jar:build hellofx.Launcher

Quite a buffer-full. The modules should make it easy. So if we do it correctly. We only need the original HelloFX.java file.

javac --module-path javafx-sdk-11.0.1/lib --add-modules javafx.controls -d build HelloFX.java

To run:

java --module-path javafx-sdk-11.0.1/lib --add-modules javafx.controls -cp build hellofx.HelloFX

So easy, and yet it can be simpler. Now we need another java file though.

module-info.java

module hellofx {
    requires javafx.controls;

    exports hellofx;
}

Now compiling is a little easier

javac --module-path javafx-sdk-11.0.1/lib/ -d build HelloFX.java module-info.java

So is running

java --module-path javafx-sdk-11.0.1/lib:build -m hellofx/hellofx.HelloFX

Finally there is jlink for bundling

  jlink --module-path jmods:build --add-modules=hellofx --output hellofx

Where jmods is the jmods package where the sdk was acquired.

  ./hellofx/bin/java -m hellofx/hellofx.HelloFX

Comments

Name: