0

I have build an executable javaFX fat jar using java 17 and javafx 18: enter image description here

The JavaFXLauncher class calls the Main class which is necessary so that the executable JavaFX jar will work. It looks like this:

    public class Main extends Application {

    @Override
    public void start(Stage stage) {
        VBox vBox = new VBox();
        Button okButton = new Button("OK");
        okButton.setOnAction(e -> new Alert(Alert.AlertType.INFORMATION).showAndWait());

        TextField searchTextField = new TextField();
        vBox.getChildren().addAll(searchTextField);

        ContextMenu contextMenu = new ContextMenu();

        Trie<String, Integer> trie = new PatriciaTrie<>();
        trie.put("calorimetru", 0);
        trie.put("calamar", 0);
        trie.put("abecedar", 0);
        trie.put("calorie", 0);

        searchTextField.setOnKeyTyped(e -> {
            TextField self = (TextField) e.getSource();

            if (StringUtils.isEmpty(self.getText())) {
                contextMenu.hide();
                return;
            }

            Set<String> items = trie.prefixMap(self.getText()).keySet();
            if (!items.isEmpty()) {
                Bounds boundsInScreen = self.localToScreen(self.getBoundsInLocal());
                int height = (int) self.getHeight();
                contextMenu.getItems().clear();
                items.forEach(item -> contextMenu.getItems().add(new MenuItem(item)));
                contextMenu.show(self, boundsInScreen.getMinX(), boundsInScreen.getMinY() + height);
            }

        });

        Scene scene = new Scene(vBox, 500, 500, Color.WHEAT);
        stage.setScene(scene);
        stage.show();
    }

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

I have built a native image using native-image like this:

    c:\Users\adragomir\IdeaProjects\learnLambda\target>native-image.cmd -jar learnLambda-1.0.jar learn.lambda.JavaFXLauncher --no-fallback
========================================================================================================================
GraalVM Native Image: Generating 'learn.lambda.JavaFXLauncher' (executable)...
========================================================================================================================
[1/7] Initializing...                                                                                   (26,7s @ 0,14GB)
 Version info: 'GraalVM 22.1.0 Java 17 CE'
 C compiler: cl.exe (microsoft, x64, 19.32.31332)
 Garbage collector: Serial GC
[2/7] Performing analysis...  [******]                                                                  (24,5s @ 0,58GB)
   4.048 (75,16%) of  5.386 classes reachable
   5.312 (52,92%) of 10.037 fields reachable
  17.728 (44,22%) of 40.090 methods reachable
      43 classes,     0 fields, and   526 methods registered for reflection
      62 classes,    54 fields, and    51 methods registered for JNI access
[3/7] Building universe...                                                                               (2,1s @ 0,85GB)
[4/7] Parsing methods...      [*]                                                                        (1,7s @ 1,15GB)
[5/7] Inlining methods...     [****]                                                                     (2,1s @ 1,20GB)
[6/7] Compiling methods...    [****]                                                                    (18,9s @ 2,68GB)
[7/7] Creating image...                                                                                  (3,7s @ 0,60GB)
   6,43MB (40,03%) for code area:   10.265 compilation units
   8,43MB (52,52%) for image heap:   2.558 classes and 120.816 objects
   1,20MB ( 7,45%) for other data
  16,06MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 packages in code area:                               Top 10 object types in image heap:
 743,04KB java.util                                            1,29MB byte[] for code metadata
 376,80KB java.lang                                            1,14MB byte[] for general heap data
 359,01KB javafx.css                                           1,12MB java.lang.String
 346,56KB com.oracle.svm.jni                                 929,29KB java.lang.Class
 276,85KB java.text                                          725,44KB byte[] for java.lang.String
 238,47KB com.oracle.svm.core.reflect                        403,36KB java.util.HashMap$Node
 238,30KB java.util.regex                                    316,25KB com.oracle.svm.core.hub.DynamicHubCompanion
 205,60KB java.util.concurrent                               247,83KB java.util.concurrent.ConcurrentHashMap$Node
 149,52KB java.math                                          221,23KB java.lang.String[]
 148,15KB com.oracle.svm.core.code                           188,09KB java.util.HashMap$Node[]
      ... 179 additional packages                                 ... 1052 additional object types
                                           (use GraalVM Dashboard to see all)
------------------------------------------------------------------------------------------------------------------------
                        2,6s (3,2% of total time) in 22 GCs | Peak RSS: 3,73GB | CPU load: 4,36
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 c:\Users\adragomir\IdeaProjects\learnLambda\target\learn.lambda.JavaFXLauncher.exe (executable)
 c:\Users\adragomir\IdeaProjects\learnLambda\target\awt.dll (jdk_lib)
 c:\Users\adragomir\IdeaProjects\learnLambda\target\java.dll (jdk_lib_shim)
 c:\Users\adragomir\IdeaProjects\learnLambda\target\jvm.dll (jdk_lib_shim)
 c:\Users\adragomir\IdeaProjects\learnLambda\target\learn.lambda.JavaFXLauncher.build_artifacts.txt
========================================================================================================================
Finished generating 'learn.lambda.JavaFXLauncher' in 1m 22s.

Everything seems fine but when I try to run the exe I get:

    $ ./learn.lambda.JavaFXLauncher.exe
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: learn.lambda.Main
        at javafx.application.Application.launch(Application.java:314)
        at learn.lambda.Main.main(Main.java:67)
        at learn.lambda.JavaFXLauncher.main(JavaFXLauncher.java:5)
Caused by: java.lang.ClassNotFoundException: learn.lambda.Main
        at java.lang.Class.forName(DynamicHub.java:1121)
        at javafx.application.Application.launch(Application.java:302)
        ... 2 more

I am using --no-fallback because I don't want to be dependent on a jvm, so I just want to run the exe by itself.

Do you guys have some idea on what could be wrong?

Regards,

adragomir
  • 457
  • 4
  • 16
  • 33

1 Answers1

1

It seems the problem is missing reflection configuration. GraalVM static analyzer cannot figure out which features are accessed reflectively, so it relies on configuration files. You can create one manually as described here, or generate with GraalVM tracing agent as described here.

peterz
  • 306
  • 1
  • 3