diff --git a/java/src/main/java/ai/onnxruntime/OnnxRuntime.java b/java/src/main/java/ai/onnxruntime/OnnxRuntime.java index 573467db05cda..430c8fcc58a54 100644 --- a/java/src/main/java/ai/onnxruntime/OnnxRuntime.java +++ b/java/src/main/java/ai/onnxruntime/OnnxRuntime.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Licensed under the MIT License. */ package ai.onnxruntime; @@ -84,6 +84,9 @@ final class OnnxRuntime { /** The available runtime providers */ static EnumSet providers; + /** The version string. */ + private static String version; + private OnnxRuntime() {} /* Computes and initializes OS_ARCH_STR (such as linux-x64) */ @@ -124,6 +127,7 @@ private static String initOsArch() { * Loads the native C library. * * @throws IOException If it can't write to disk to copy out the library from the jar file. + * @throws IllegalStateException If the native library failed to load. */ static synchronized void init() throws IOException { if (loaded) { @@ -139,7 +143,11 @@ static synchronized void init() throws IOException { load(ONNXRUNTIME_LIBRARY_NAME); load(ONNXRUNTIME_JNI_LIBRARY_NAME); ortApiHandle = initialiseAPIBase(ORT_API_VERSION_11); + if (ortApiHandle == 0L) { + throw new IllegalStateException("Failed to load native library"); + } providers = initialiseProviders(ortApiHandle); + version = initialiseVersion(); loaded = true; } finally { if (tempDirectory != null) { @@ -161,6 +169,15 @@ private static void cleanUp(File file) { file.deleteOnExit(); } + /** + * Gets the native library version string. + * + * @return The version string. + */ + static String version() { + return version; + } + /** * Extracts the CUDA provider library from the classpath resources if present, or checks to see if * the CUDA provider library is in the directory specified by {@link #ONNXRUNTIME_NATIVE_PATH}. @@ -433,4 +450,11 @@ private static EnumSet initialiseProviders(long ortApiHandle) { * @return The array of providers */ private static native String[] getAvailableProviders(long ortApiHandle); + + /** + * Gets the version string from the native library. + * + * @return The version string. + */ + private static native String initialiseVersion(); } diff --git a/java/src/main/java/ai/onnxruntime/OrtEnvironment.java b/java/src/main/java/ai/onnxruntime/OrtEnvironment.java index 090fe34a45b6f..73718093c6b41 100644 --- a/java/src/main/java/ai/onnxruntime/OrtEnvironment.java +++ b/java/src/main/java/ai/onnxruntime/OrtEnvironment.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved. * Licensed under the MIT License. */ package ai.onnxruntime; @@ -271,9 +271,24 @@ public void setTelemetry(boolean sendTelemetry) throws OrtException { setTelemetry(OnnxRuntime.ortApiHandle, nativeHandle, sendTelemetry); } + /** + * Gets the native library version string. + * + * @return The version string. + */ + public String getVersion() { + return OnnxRuntime.version(); + } + @Override public String toString() { - return "OrtEnvironment(name=" + curLoggingName + ",logLevel=" + curLogLevel + ")"; + return "OrtEnvironment(name=" + + curLoggingName + + ",logLevel=" + + curLogLevel + + ",version=" + + getVersion() + + ")"; } /** diff --git a/java/src/main/native/ai_onnxruntime_OnnxRuntime.c b/java/src/main/native/ai_onnxruntime_OnnxRuntime.c index 911651dae8075..a209f2df2b045 100644 --- a/java/src/main/native/ai_onnxruntime_OnnxRuntime.c +++ b/java/src/main/native/ai_onnxruntime_OnnxRuntime.c @@ -1,8 +1,9 @@ /* - * Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved. * Licensed under the MIT License. */ #include +#include #include "onnxruntime/core/session/onnxruntime_c_api.h" #include "ai_onnxruntime_OnnxRuntime.h" #include "OrtJniUtil.h" @@ -54,3 +55,17 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxRuntime_getAvailableProvi } return providerArray; } + +/* + * Class: ai_onnxruntime_OnnxRuntime + * Method: initialiseVersion + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_ai_onnxruntime_OnnxRuntime_initialiseVersion + (JNIEnv * jniEnv, jclass clazz) { + (void)clazz; // required JNI parameter not needed by functions which don't access their host class. + const char* version = OrtGetApiBase()->GetVersionString(); + assert(version != NULL); + jstring versionStr = (*jniEnv)->NewStringUTF(jniEnv, version); + return versionStr; +} diff --git a/java/src/test/java/ai/onnxruntime/InferenceTest.java b/java/src/test/java/ai/onnxruntime/InferenceTest.java index cdc94e3991f89..d545292aead80 100644 --- a/java/src/test/java/ai/onnxruntime/InferenceTest.java +++ b/java/src/test/java/ai/onnxruntime/InferenceTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Licensed under the MIT License. */ package ai.onnxruntime; @@ -72,6 +72,12 @@ public void environmentTest() { assertSame(env, otherEnv); } + @Test + public void testVersion() { + String version = env.getVersion(); + Assertions.assertFalse(version.isEmpty()); + } + @Test public void createSessionFromPath() throws OrtException { String modelPath = TestHelpers.getResourcePath("/squeezenet.onnx").toString();