Skip to content

Commit 54924d4

Browse files
guanzhiclaude
andcommitted
Fix NativeLoader Windows DLL loading issues
- Remove finally block that deletes temp DLL file after loading, which is problematic on Windows where loaded DLLs are locked - Add Windows support in checkReferencedLib() to pre-load gmssl.dll before libgmssljni.dll, matching macOS behavior - Refactor checkReferencedLib() to handle both macOS and Windows Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent c1fd176 commit 54924d4

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

src/main/java/org/gmssl/NativeLoader.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ public static void load(String library) {
7373
throw new GmSSLException("Failed to load native library:"+ e.getMessage());
7474
} catch (Exception e) {
7575
throw new GmSSLException("Unable to load lib!");
76-
}finally {
77-
if (null != tempFile) {
78-
tempFile.toFile().delete();
79-
}
8076
}
8177
}
8278

@@ -137,30 +133,34 @@ static String libExtension() {
137133
* This has already been loaded and manual execution is unnecessary.
138134
*/
139135
private static void checkReferencedLib() {
140-
if ("osx".equals(osType())) {
141-
String macReferencedLib = referencedLibFromGmSSLRoot();
142-
if (macReferencedLib == null || macReferencedLib.isEmpty()) {
143-
macReferencedLib = PROPERTIES.getProperty("macReferencedLib");
144-
}
145-
if (macReferencedLib != null && !macReferencedLib.isEmpty()) {
146-
File libFile = new File(macReferencedLib);
147-
if (libFile.exists()) {
148-
System.load(macReferencedLib);
149-
}
150-
}
151-
}
152-
}
153-
154-
private static String referencedLibFromGmSSLRoot() {
155136
String gmsslRoot = System.getProperty("gmssl.root");
156137
if (gmsslRoot == null || gmsslRoot.isEmpty()) {
157138
gmsslRoot = System.getenv("GMSSL_ROOT");
158139
}
159140
if (gmsslRoot == null || gmsslRoot.isEmpty()) {
160-
return null;
141+
return;
142+
}
143+
144+
String os = osType();
145+
if ("osx".equals(os)) {
146+
// Pre-load libgmssl.3.dylib so that @rpath resolution works
147+
String macReferencedLib = PROPERTIES.getProperty("macReferencedLib");
148+
if (macReferencedLib == null || macReferencedLib.isEmpty()) {
149+
Path libPath = Paths.get(gmsslRoot, "lib", "libgmssl.3.dylib");
150+
macReferencedLib = libPath.toString();
151+
}
152+
File libFile = new File(macReferencedLib);
153+
if (libFile.exists()) {
154+
System.load(macReferencedLib);
155+
}
156+
} else if ("win".equals(os)) {
157+
// Pre-load gmssl.dll so that the JNI DLL can resolve its dependency
158+
Path libPath = Paths.get(gmsslRoot, "bin", "gmssl.dll");
159+
File libFile = libPath.toFile();
160+
if (libFile.exists()) {
161+
System.load(libPath.toString());
162+
}
161163
}
162-
Path libPath = Paths.get(gmsslRoot, "lib", "libgmssl.3.dylib");
163-
return libPath.toString();
164164
}
165165

166166
}

0 commit comments

Comments
 (0)