Class NativeLibrary
Both the classic ONNX Runtime bindings
(smile.onnx) and the GenAI bindings (smile.onnx.genai) need the
same onnxruntime shared library loaded before the jextract-generated
foreign bindings perform their lookup. This class is the single place that
resolves, guards, and loads it, so the two paths cannot drift apart.
Why an explicit absolute-path load at all
The generated bindings load by bare library name
(SymbolLookup.libraryLookup(System.mapLibraryName("onnxruntime"))),
which delegates to the operating system's usual search. On Windows an older
onnxruntime.dll shipped in C:\Windows\System32 wins that
search over a pip-installed copy on PATH — the system directory
precedes user directories in the default DLL search order. Loading the
intended copy first, by absolute path, binds the process to it and
makes the later bare-name lookup resolve to the already-loaded copy. On most
Unix-like systems loading the library first is also required because its
dependencies are not on the loader's search path.
Resolution order
A library directory is resolved from, in order:
- a JVM system property (e.g.
-Donnxruntime.native.path) - an environment variable (e.g.
ONNXRUNTIME_NATIVE_PATH) - directories on
PATH/LD_LIBRARY_PATH/DYLD_LIBRARY_PATHthat contain the mapped library file
The search-path scan deliberately skips System32 and
SysWOW64, so a stale Windows system copy is never treated as the
intended one.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringBare (unmapped) name of the ONNX Runtime GenAI library.static final StringEnvironment variable counterpart ofGENAI_NATIVE_PATH_PROPERTY.static final StringSystem property naming the directory that containsonnxruntime-genai(environment variableONNXRUNTIME_GENAI_NATIVE_PATH).static final StringBare (unmapped) name of the ONNX Runtime library.static final StringEnvironment variable counterpart ofORT_NATIVE_PATH_PROPERTY.static final StringSystem property naming the directory that containsonnxruntime(environment variableONNXRUNTIME_NATIVE_PATH). -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanensureLoaded(String property, String env, String bareName) Loads a native library by absolute path, at most once per library stem.static StringfindLibraryFile(String bareName) Finds a mapped library file under the resolved native directories or on the OS library search path.static booleanlibraryFilePresent(String bareName) Returns whether a mapped library file is present (loaded or not).static StringresolveDir(String property, String env, String bareName) Resolves the directory holding a mapped native library.
-
Field Details
-
ORT_NATIVE_PATH_PROPERTY
System property naming the directory that containsonnxruntime(environment variableONNXRUNTIME_NATIVE_PATH).- See Also:
-
GENAI_NATIVE_PATH_PROPERTY
System property naming the directory that containsonnxruntime-genai(environment variableONNXRUNTIME_GENAI_NATIVE_PATH).- See Also:
-
ORT_NATIVE_PATH_ENV
Environment variable counterpart ofORT_NATIVE_PATH_PROPERTY.- See Also:
-
GENAI_NATIVE_PATH_ENV
Environment variable counterpart ofGENAI_NATIVE_PATH_PROPERTY.- See Also:
-
ORT_LIBRARY
-
GENAI_LIBRARY
Bare (unmapped) name of the ONNX Runtime GenAI library.- See Also:
-
-
Method Details
-
resolveDir
Resolves the directory holding a mapped native library.An explicitly configured system property or environment variable wins outright, even if the named library is not actually there: silently falling back to a different copy found on the search path would be worse than letting the load fail with an honest "not found". The search path is consulted only when neither is set.
- Parameters:
property- the JVM system property name (may benull).env- the environment variable name (may benull).bareName- the bare library stem, e.g.onnxruntime.- Returns:
- the absolute directory path, or
nullwhen not found.
-
ensureLoaded
Loads a native library by absolute path, at most once per library stem.Safe to call from several classes' static initializers: the first call performs the load, later calls are no-ops. A failure to load is swallowed — the caller's own native lookup will produce a clearer error than this best-effort preload could.
- Parameters:
property- the JVM system property name (may benull).env- the environment variable name (may benull).bareName- the bare library stem, e.g.onnxruntime.- Returns:
truewhen the library is loaded after this call.
-
findLibraryFile
-
libraryFilePresent
Returns whether a mapped library file is present (loaded or not).- Parameters:
bareName- the bare library stem, e.g.onnxruntime_providers_cuda.- Returns:
truewhen the file exists somewhere resolvable.
-