Skip to content

Commit c0fd2f7

Browse files
authored
Java: avoid provider resource extraction when library already exists in onnxruntime.native.path (microsoft#27668)
## Summary Fixes the Java native provider loading flow so that `extractProviderLibrary` checks `onnxruntime.native.path` before attempting extraction from classpath resources. Previously, when a provider library was already present in `onnxruntime.native.path`, the Java loader could still attempt `extractFromResources(...)` first, and only fall back to the configured native path afterwards. This caused an unnecessary extraction attempt even though the library was already available on disk. This change updates the lookup order to: 1. Return immediately if the provider was already marked as ready 2. Check whether the provider library already exists in `onnxruntime.native.path` 3. Only if not found there, attempt extraction from classpath resources ## Tests Added a regression test covering the case where the requested provider library already exists in `onnxruntime.native.path`. The test verifies that: - `extractProviderLibrary(...)` returns `true` - extraction from resources is not attempted in that case A small test-only hook was added to observe calls to `extractFromResources(...)` so the regression can be validated directly and deterministically. ## Issue Fixes microsoft#27655
1 parent 44e8b38 commit c0fd2f7

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

java/src/main/java/ai/onnxruntime/OnnxRuntime.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,26 +307,22 @@ static synchronized boolean extractProviderLibrary(String libraryName) {
307307
if (extractedSharedProviders.contains(libraryName)) {
308308
return true;
309309
}
310-
// Otherwise extract the file from the classpath resources
310+
// If a native library directory is configured, prefer it and skip extraction when present.
311+
if (libraryDirPathProperty != null) {
312+
String libraryFileName = mapLibraryName(libraryName);
313+
File libraryFile = Paths.get(libraryDirPathProperty, libraryFileName).toFile();
314+
if (libraryFile.exists()) {
315+
extractedSharedProviders.add(libraryName);
316+
return true;
317+
}
318+
}
319+
// Otherwise extract the file from the classpath resources.
311320
Optional<File> file = extractFromResources(libraryName);
312321
if (file.isPresent()) {
313322
extractedSharedProviders.add(libraryName);
314323
return true;
315324
} else {
316-
// If we failed to extract it, check if there is a valid cache directory
317-
// that contains it
318-
if (libraryDirPathProperty != null) {
319-
String libraryFileName = mapLibraryName(libraryName);
320-
File libraryFile = Paths.get(libraryDirPathProperty, libraryFileName).toFile();
321-
if (libraryFile.exists()) {
322-
extractedSharedProviders.add(libraryName);
323-
return true;
324-
} else {
325-
return false;
326-
}
327-
} else {
328-
return false;
329-
}
325+
return false;
330326
}
331327
}
332328

0 commit comments

Comments
 (0)