graalvm

How can you tell if your Java program is running in a GraalVM AOT context?


I have a little Java program. I build a binary using Graal's native-image (i.e. GraalVM AOT aka SubstrateVM).

My program can be executed either with a Java runtime or from the native-image binary. What's the best way to tell which context I'm running in?

(This might be a bad practice in general but I believe it's inevitable/necessary in certain not-uncommon circumstances.)


Solution

  • GraalVM now provides an API for checking the AOT context:

    ImageInfo.inImageCode()
    ImageInfo.inImageRuntimeCode()
    ImageInfo.inImageBuildtimeCode()
    ImageInfo.isExecutable()
    ImageInfo.isSharedLibrary()