davidbak
davidbak

Reputation: 6029

How can I tell - in a standard Java program - if I'm running in GraalVM or not?

I would like to enable my application to have additional features if the user is running under GraalVM. Since these days most people are not running GraalVM it is an extra set of steps (and a burden) to get it going just to run my application, so I don't want to require GraalVM. I want to just know I'm running under it so I can dynamically load some classes and/or choose to use some features.

Question is: How do I do this in some kind of "official" way? (I could just try to load the classes I want and see what happens, is that the best way?)

Upvotes: 2

Views: 446

Answers (2)

Oleg Šelajev
Oleg Šelajev

Reputation: 3800

To check if your code is running on GraalVM you can use the following method from the graal-sdk: Version.isRelease()

org.graalvm.home.Version.getCurrent().isRelease()

It's available in the library:

<dependency>
    <groupId>org.graalvm.sdk</groupId>
    <artifactId>graal-sdk</artifactId>
    <version>20.2.0</version>
</dependency>

However your question could be about whether you're running in the native image or using the jit mode (I assume, because you talk about loading classes, etc).

In this case you want to use the ImageInfo class: https://www.graalvm.org/sdk/javadoc/index.html?org/graalvm/nativeimage/package-summary.html

which has methods like: inImageBuildtimeCode() -- during the native image build inImageRuntimeCode() -- during the runtime of a built native image, your code probably shouldn't load classes if this is true

etc.

The implementation sets/reads the PROPERTY_IMAGE_CODE_KEY property, so you can also check the value yourself if you don't want to bring in an extra jar.

Upvotes: 1

David P. Caldwell
David P. Caldwell

Reputation: 3829

I'd probably do something like

boolean isGraalVm = System.getProperty("org.graalvm.home") != null;

Upvotes: 1

Related Questions