GraalVM Error With Solution (JNI)
Published 08/30/2025
I use GraalVM a lot. One day I decided to see if GraalVM had a reddit - it does! It’s fairly inactive, but I came across a thread from 10 months ago: GraalVM beginner - no errors compiling but exe crashes - what am I doing wrong?
The error:
Exception in thread "main" java.lang.NoSuchMethodError: java.awt.Toolkit.getDefaultToolkit()Ljava/awt/Toolkit;
at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1848)
This means java/awt/Toolkit is missing from the jni_config.json - that’s all. This file is automatically read from META-INF/native-image/jni_config.json
The fix:
{
"name":"java.awt.Toolkit",
"methods":[
{"name":"getDefaultToolkit","parameterTypes":[] },
{"name":"getFontMetrics","parameterTypes":["java.awt.Font"] }
]
},
Obviously this is going to be JDK and project dependent - but you get the gist.
If you’re not exactly sure what variables or methods are used, here’s a catch-all you can use:
{
"name": "java.awt.Toolkit",
"allDeclaredMethods": true,
"allPublicMethods": true,
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredFields": true,
"allPublicFields": true
},
You’re probably wondering “Can’t I just generate this automatically” and the anwser is yes - using native-image-agent you can profile your application, and dump this data without writing a single line by hand. There is no need to write it by hand - unless native-image-agent isn’t compatible with your application…Ask me how I know.
To automatically capture JNI / reflection usage, use the following command:
java -agentlib:native-image-agent=config-merge-dir=src/main/resources/META-INF/native-image -jar <your-application.jar>
You can tweak the command, it can work with classpath if you don’t have a built jar. The main point is including -agentlib:native-image-agent
Hopefully this helps explain some of the cryptic errors you get while using GraalVM!