Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime OnnxRuntime Java

From Leeroopedia


Knowledge Sources Description
Source File java/src/main/java/ai/onnxruntime/OnnxRuntime.java
Repository Microsoft/onnxruntime

Domains

  • Machine Learning Runtime
  • JNI Native Library Loading
  • Platform Detection

Overview

OnnxRuntime is a package-private final class responsible for loading the ONNX Runtime native libraries (JNI bindings) into the JVM. It detects the operating system and CPU architecture, extracts shared libraries from classpath resources or specified paths, and initializes the ORT C API handles. This class is not part of the public API; it is called internally by other classes in the ai.onnxruntime package to ensure native libraries are loaded before any ONNX Runtime operations are performed.

Description

The OnnxRuntime class serves as the static bootstrap for all native library loading in the Java ONNX Runtime binding. It handles:

  • Platform detection: Identifies OS (Windows, macOS, Linux, Android) and architecture (x64, aarch64, ppc64, loongarch64) at class load time.
  • Library extraction: Extracts native shared libraries from JAR resources into a temporary directory, or loads them from user-specified paths via the onnxruntime.native.path system property.
  • API initialization: Obtains native pointers to the ORT API (version 23), the Training API, and the Compile API.
  • Provider discovery: Queries available execution providers (CUDA, TensorRT, OpenVINO, DNNL, ROCm, QNN, WebGPU, etc.) and stores them in an EnumSet<OrtProvider>.
  • Shared provider extraction: Provides methods like extractCUDA(), extractROCM(), extractDNNL(), extractOpenVINO(), extractTensorRT(), and extractQNN() to prepare shared provider libraries for ORT to load.

The class uses a synchronized init() method to guarantee thread-safe, one-time initialization.

Usage

This class is package-private and cannot be instantiated or called directly by user code. It is triggered automatically when OrtEnvironment or other public ONNX Runtime classes are first accessed.

Code Reference

Source Location

// File: java/src/main/java/ai/onnxruntime/OnnxRuntime.java
// Package: ai.onnxruntime

Signature

final class OnnxRuntime {
    static final String ONNXRUNTIME_NATIVE_PATH = "onnxruntime.native.path";
    static long ortApiHandle;
    static long ortTrainingApiHandle;
    static long ortCompileApiHandle;
    static boolean trainingEnabled;
    static EnumSet<OrtProvider> providers;

    static synchronized void init() throws IOException;
    static String version();
    static boolean extractCUDA();
    static boolean extractROCM();
    static boolean extractDNNL();
    static boolean extractOpenVINO();
    static boolean extractTensorRT();
    static boolean extractQNN();
    static synchronized boolean extractProviderLibrary(String libraryName);
    static boolean isAndroid();
}

Import

// Package-private class, not importable by external code

I/O Contract

Inputs

Name Type Description
(System property) onnxruntime.native.path String Optional path to a directory containing native libraries
ORT_API_VERSION_23 int The ORT C API version to initialize against

Outputs

Name Type Description
ortApiHandle long Native pointer to the ORT API struct
ortTrainingApiHandle long Native pointer to the Training API struct (0 if training not available)
ortCompileApiHandle long Native pointer to the Compile API struct
providers EnumSet<OrtProvider> Set of available execution providers
version String The ONNX Runtime version string

Usage Examples

// OnnxRuntime is used internally. Users interact via OrtEnvironment:
OrtEnvironment env = OrtEnvironment.getEnvironment();
// At this point, OnnxRuntime.init() has already been called,
// loading native libraries, initializing API handles, and
// discovering available providers.
System.out.println("ORT Version: " + env.getVersion());
System.out.println("Available providers: " + OrtEnvironment.getAvailableProviders());

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment