Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graal native-image of swing app: Detected a started Thread in the image heap

public class SimpleApp {
    public static void main( String[] args ) {
        JOptionPane.showMessageDialog(null, "Ciao", "Info", JOptionPane.INFORMATION_MESSAGE);
    }
} 

when I try to create native image with native-image --no-fallback I get this:

Error: Detected a started Thread in the image heap. Threads running in the image generator are no longer running at image run time. The object was probably created by a class initializer and is reachable from a static field. By default, all class initialization is done during native image building.You can manually delay class initialization to image run time by using the option -H:ClassInitialization=. Or you can write your own initialization methods and call them explicitly from your main entry point.

Detailed message: Trace: object sun.java2d.opengl.OGLRenderQueue field sun.java2d.opengl.OGLRenderQueue.theInstance

like image 853
Alf Avatar asked Oct 16 '22 13:10

Alf


1 Answers

This is all related to when a class should be initialized -- there is an updated article from the GraalVM team on this subject: https://medium.com/graalvm/updates-on-class-initialization-in-graalvm-native-image-generation-c61faca461f7

Short version: use tracing (added in a newer version of the Graal package than yours) to find what called the problematic class, and then set those to be initialized at run time. Easy to say, harder to do. For what it's worth, I just hit the same problem with the 19.2 (enterprise) release and some random code that included AWT. The message has changed, though (slightly edited for clarity):

Error: Detected a started Thread in the image heap. Threads running in the image generator are no longer running at image run time. To see how this object got instantiated use -H:+TraceClassInitialization. The object was probably created by a class initializer and is reachable from a static field. You can request class initialization at image run time by using the option --initialize-at-build-time=<class-name>. Or you can write your own initialization methods and call them explicitly from your main entry point. Trace: object sun.awt.AWTAutoShutdown method sun.awt.AWTAutoShutdown.getInstance()

I'll be working through this (possibly starting with the AWT equivalent of "Hello World" and will update my answer with concrete details if I succeed. Google is littered with examples of native images not working with Swing/AWT/JavaFX, so I'm not sure I will be successful.

Disclaimer: I work for Oracle, but not in an organization closely connected to the Graal developers (probably no managers in common). Anything I say is my opinion.

like image 146
NerdDad Avatar answered Oct 21 '22 01:10

NerdDad