javajavafxagentgraalvm-native-image

JavaFX Preloader: Expected static method javafx.graphics/com.sun.javafx.iio.ImageStorage.addImageLoaderFactory


In order to create a native image with GraalVM I must first create a lib-agent. I use this command line:

java -agentlib:native-image-agent=config-output-dir="META-INF/native-image" --module-path %MODULE_PATH% --add-modules ALL-DEFAULT,ALL-SYSTEM,ALL-MODULE-PATH --add-exports javafx.graphics/com.sun.javafx.application=ALL-UNNAMED --add-exports javafx.graphics/com.sun.javafx.iio=ALL-UNNAMED -cp "classes;lib/*" app.Main

But I get this error :

Exception in Preloader start method
Exception in thread "main" java.lang.RuntimeException: Exception in Preloader start method
        at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
        at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
        at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.IncompatibleClassChangeError: Expected static method 'void com.sun.javafx.iio.ImageStorage.addImageLoaderFactory(com.sun.javafx.iio.ImageLoaderFactory)'
        at de.codecentric.centerdevice.javafxsvg.SvgImageLoaderFactory.install(SvgImageLoaderFactory.java:15)
        at app.internal.view.preloader.FxAppPreloader.start(FxAppPreloader.java:114)
        at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:761)
        at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
        at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
        at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
        at javafx.graphics@21.0.3/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at javafx.graphics@21.0.3/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics@21.0.3/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
        ... 1 more

According to various solutions on the internet I tried to compile the program and use other commands like --enable-native-access but this gave no change.

Thanks you in advance for your responses !


Solution

  • This has nothing to do with Graal or JavaFX Preloaders.

    The library you are using (javafxsvg) hasn't been updated in six years and is incompatible with modern JavaFX versions.

    The method being invoked by the library:

    is not a publicly supported JavaFX API, so it is subject to change at any time without notification.

    The addImageLoaderFactory method is not a static method (as the javafxsvg library is currently coded to assume). The ImageStorage class uses a Singleton instance pattern instead. The code was changed as part of JDK-8277572 ImageStorage should correctly handle MIME types for images encoded in data URIs

    Potential Fixes

    To address your error, you need to use a different library to accomplish the same functionality (SVG display in JavaFX). I have no advice on other SVG libraries for JavaFX with similar functionality.

    Or copy the library code yourself, fix it to work with modern JavaFX versions, and rebuild it.

    Alternatively, you could try to find a version of JavaFX that is compatible with your library choice (perhaps JavaFX 17).