3

I have a modular JavaFX application with the following Java code:

package webbrowser;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebView;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;

public class WebBrowser extends Application {

    @Override
    public void start(Stage stage) {
        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();
        webEngine.load("http://www.oracle.com");
        Scene scene = new Scene(browser, 500, 500);
        stage.setScene(scene);
        stage.show();
    }

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

}

Here is the module-info.java file:

module webBrowser {
    requires javafx.controls;
    requires javafx.web;
    
    exports webbrowser;
}

I compile the modular JavaFX application with the following command:

javac -d out --module-source-path . --module-path $JAVAFX_SDK/libs:$JAVAFX_JMODS --module webBrowser

I package the modular JavaFX application with the following command:

jpackage --type dmg --module-path $JAVAFX_JMODS:$JAVAFX_SDK/lib:out --add-modules javafx.controls,javafx.web,javafx.graphics,javafx.media --module webBrowser/webbrowser.WebBrowser

Then I save the application in my /Applications folder (I'm on Mac OS) and open the application. The application opens a 500x500 window, but it doesn't render any graphics, and it doesn't open a web browser. The application just opens a white blank 500x500 window.

Why isn't the application running properly?

When I run the application with java (and forego jpackage) the application runs fine in the JVM, opens a web browser, and opens the oracle.com webpage.

java --module-path out:$JAVAFX_JMODS:$JAVAFX_SDK/lib --add-modules javafx.controls,javafx.web --module webBrowser/webbrowser.WebBrowser

Why doesn't the application work when I use jpackage, install the dmg and run the Mac OS app?

ktm5124
  • 11,861
  • 21
  • 74
  • 119
  • 1
    Which version of JavaFX are you using? – VGR Feb 07 '22 at 04:07
  • 2
    Use only the jmods dir for jpackage/jlink, and only lib folder for java/javac. Don't forget to try `--win-console`, see info [here](https://stackoverflow.com/questions/70456027/java-fx-application-with-jpackage-not-work/70460422#70460422) – DuncG Feb 07 '22 at 15:28

1 Answers1

2

The section of the openjfx.io documentation on runtime images, modular from CLI, demonstrates some of the points outlined below and provides examples of the command line arguments to use for compiling, linking, packaging and executing the application.

I suggest you get the linked openjfx demo working in your environment, then try the same for your project.

Some issues to address:

  1. jmods can't be used at runtime, only at compile or link time.

  2. --add-modules is not required for java/javac when you have a module-info.java.

    • for jpackage just list your app module in --add-modules, you don't need to list the javafx package because it can work that out from your module-info.
  3. Your lib directory is inconsistent, always use the same (correct) directory:

    • sometimes you use lib and sometimes you use libs.
  4. Provide an onError handler.

  5. When using jpackage you should have only the JavaFX mods.

    • you don't need the mods and SDK lib, use only mods.

Further points to check are highlighted in the answer to:

jewelsea
  • 150,031
  • 14
  • 366
  • 406