LibraryUtils.java
8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.k2fsa.sherpa.onnx;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Locale;
import java.util.Objects;
/*
# We support the following loading methods
## Method 1 Specify the property sherpa_onnx.native.path
We assume the path contains the libraries sherpa-onnx-jni and onnxruntime.
java \
-Dsherpa_onnx.native.path=/Users/fangjun/sherpa-onnx/build/install/lib \
-cp /Users/fangjun/sherpa-onnx/sherpa-onnx/java-api/build/sherpa-onnx.jar
xxx.java
## Method 2 Specify the native jar library
java \
-cp /Users/fangjun/sherpa-onnx/sherpa-onnx/java-api/build/sherpa-onnx.jar:/path/to/sherpa-onnx-osx-x64.jar
xxx.java
Note that you need to replace : in -cp with ; on windows.
## Method 3 Specify the property java.library.path
We assume the path contains the libraries sherpa-onnx-jni and onnxruntime.
java \
-Djava.library.path=/Users/fangjun/sherpa-onnx/build/install/lib \
-cp /Users/fangjun/sherpa-onnx/sherpa-onnx/java-api/build/sherpa-onnx.jar
xxx.java
*/
public class LibraryUtils {
// System property to override native library path
private static final String NATIVE_PATH_PROP = "sherpa_onnx.native.path";
private static final String LIB_NAME = "sherpa-onnx-jni";
private static boolean debug = false;
private static String detectedOS;
public static void enableDebug() {
debug = true;
}
public static void disableDebug() {
debug = false;
}
public static void load() {
// 1. Try to load from external directory specified by -Dsherpa_onnx.native.path if provided
if (loadFromSherpaOnnxNativePath()) {
return;
}
// 2. Load from resources contains in some jar file
try {
if (loadFromResourceInJar()) {
return;
}
} catch (IOException e) {
// pass
}
// 3. fallback to -Djava.library.path
// java -Djava.library.path=C:\mylibs;D:\otherlibs -cp sherpa-onnx.jar xxx.java
//
// It throws if it cannot load the lib sherpa-onnx-jni
System.loadLibrary(LIB_NAME);
}
// You specify -Dsherpa_onnx.native.path=/path/to/some/dir
// where /path/to/some/dir contains the sherpa-onnx-jni and onnxruntime libs
private static boolean loadFromSherpaOnnxNativePath() {
String libFileName = System.mapLibraryName(LIB_NAME);
String nativePath = System.getProperty(NATIVE_PATH_PROP);
if (nativePath != null) {
File nativeDir = new File(nativePath);
File libInDir = new File(nativeDir, libFileName);
if (nativeDir.isDirectory() && libInDir.exists()) {
if (debug) {
System.out.printf("Loading from: %s\n", libInDir.getAbsolutePath());
}
System.load(libInDir.getAbsolutePath());
return true;
}
}
if (debug) {
System.out.println("nativePath is null");
}
return false;
}
private static boolean loadFromResourceInJar() throws IOException {
String libFileName = System.mapLibraryName(LIB_NAME);
String sherpaOnnxJniPath = "sherpa-onnx/native/" + getOsArch() + '/' + libFileName;
Path tempDirectory = null;
try {
if (!resourceExists(sherpaOnnxJniPath)) {
if (debug) {
System.out.printf("%s does not exist\n", sherpaOnnxJniPath);
}
return false;
}
tempDirectory = Files.createTempDirectory("sherpa-onnx-java");
if (Objects.equals(detectedOS, "osx")) {
// for macos, we need to first load libonnxruntime.1.17.1.dylib
String onnxruntimePath = "sherpa-onnx/native/" + getOsArch() + '/' + "libonnxruntime.1.17.1.dylib";
if (!resourceExists(onnxruntimePath)) {
if (debug) {
System.out.printf("%s does not exist\n", onnxruntimePath);
}
return false;
}
File tempFile = tempDirectory.resolve("libonnxruntime.1.17.1.dylib").toFile();
extractResource(onnxruntimePath, tempFile);
System.load(tempFile.getAbsolutePath());
} else {
String onnxLibFileName = System.mapLibraryName("onnxruntime");
String onnxruntimePath = "sherpa-onnx/native/" + getOsArch() + '/' + onnxLibFileName;
if (!resourceExists(onnxruntimePath)) {
if (debug) {
System.out.printf("%s does not exist\n", onnxruntimePath);
}
return false;
}
File tempFile = tempDirectory.resolve(onnxLibFileName).toFile();
extractResource(onnxruntimePath, tempFile);
System.load(tempFile.getAbsolutePath());
}
File tempFile = tempDirectory.resolve(libFileName).toFile();
extractResource(sherpaOnnxJniPath, tempFile);
System.load(tempFile.getAbsolutePath());
} finally {
if (tempDirectory != null) {
cleanUpTempDir(tempDirectory.toFile());
}
}
return true;
}
// this method is copied and modified from
// https://github.com/microsoft/onnxruntime/blob/main/java/src/main/java/ai/onnxruntime/OnnxRuntime.java#L118
private static String getOsArch() {
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if (os.contains("mac") || os.contains("darwin")) {
detectedOS = "osx";
} else if (os.contains("win")) {
detectedOS = "win";
} else if (os.contains("nux")) {
detectedOS = "linux";
} else {
throw new IllegalStateException("Unsupported os:" + os);
}
String detectedArch;
String arch = System.getProperty("os.arch", "generic")
.toLowerCase(Locale.ENGLISH);
if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
detectedArch = "x64";
} else if (arch.startsWith("x86")) {
// 32-bit x86 is not supported by the Java API
detectedArch = "x86";
} else if (arch.startsWith("aarch64") || arch.startsWith("arm64")) {
detectedArch = "aarch64";
} else if (arch.startsWith("arm")) {
detectedArch = "arm"; //armv8l架构
} else {
throw new IllegalStateException("Unsupported arch:" + arch);
}
return detectedOS + '-' + detectedArch;
}
private static void extractResource(String resourcePath, File destination) {
if (debug) {
System.out.printf("Copying from resource path %s to %s\n", resourcePath, destination.toPath());
}
try (InputStream in = LibraryUtils.class.getClassLoader().getResourceAsStream(resourcePath)) {
if (in == null) {
throw new RuntimeException("Resource not found: " + resourcePath);
}
Files.copy(in, destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException("Failed to extract resource " + resourcePath + " to " + destination.getAbsolutePath(), e);
}
}
// From ChatGPT:
// Class.getResourceAsStream(String path) behaves differently than ClassLoader
// - No leading slash → relative to the package of LibraryUtils
// - Leading slash → absolute path relative to classpath root
//
// ClassLoader.getResourceAsStream always uses absolute paths relative to classpath root,
// no leading slash needed
private static boolean resourceExists(String path) {
return LibraryUtils.class.getClassLoader().getResource(path) != null;
}
private static void cleanUpTempDir(File dir) {
if (!dir.exists()) return;
File[] files = dir.listFiles();
if (files != null) {
for (File f : files) {
f.deleteOnExit(); // schedule each .so for deletion
}
}
dir.deleteOnExit(); // schedule the directory itself
}
}