Skip to content

Commit f5bcbb1

Browse files
neykovclaude
andcommitted
Register attach library under short name after loading by path
System.load() registers a library by its full path, not by short name. When WindowsAttachProvider's static initializer later calls System.loadLibrary("attach"), the JVM searches java.library.path and fails because the standalone JRE bin/ has no attach.dll. After loading by absolute path, extend java.library.path to include the dll's parent directory, null ClassLoader.sys_paths to force a rescan, then call System.loadLibrary("attach") to register the short name. Any subsequent loadLibrary("attach") calls from WindowsAttachProvider then find the name already registered and succeed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f4cc2a3 commit f5bcbb1

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

src/main/java/name/neykov/secrets/cli/AttachHelper.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.sun.tools.attach.VirtualMachineDescriptor;
88
import java.io.File;
99
import java.io.IOException;
10+
import java.lang.reflect.Field;
1011

1112
/**
1213
* A companion to AgentAttach that needs to be loaded in a different class loader, due to the
@@ -152,8 +153,35 @@ private static boolean tryLoadLibrary(String relativePath) throws FailureMessage
152153
} catch (UnsatisfiedLinkError e) {
153154
return false;
154155
}
155-
// Once loaded by absolute path the library is registered under the name "attach",
156-
// so any subsequent System.loadLibrary("attach") calls will find it already loaded.
156+
// System.load() registers the library by its full path, not by short name.
157+
// WindowsAttachProvider's static initializer calls System.loadLibrary("attach"),
158+
// which searches java.library.path. Extend the path to include the library's
159+
// parent directory and reset the classloader's cached sys_paths so the JVM
160+
// rescans it, then call System.loadLibrary("attach") to register the short name.
161+
String libDir = lib.getParent();
162+
String initialPath = System.getProperty("java.library.path");
163+
String extendedPath =
164+
(initialPath != null && !initialPath.isEmpty())
165+
? initialPath + File.pathSeparator + libDir
166+
: libDir;
167+
System.setProperty("java.library.path", extendedPath);
168+
Field sysPathsField;
169+
try {
170+
sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
171+
} catch (NoSuchFieldException e) {
172+
return false;
173+
}
174+
sysPathsField.setAccessible(true);
175+
try {
176+
sysPathsField.set(null, null);
177+
} catch (IllegalAccessException e) {
178+
return false;
179+
}
180+
try {
181+
System.loadLibrary("attach");
182+
} catch (UnsatisfiedLinkError e) {
183+
return false;
184+
}
157185
return true;
158186
}
159187
}

0 commit comments

Comments
 (0)