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
| public class LibUtil {
private final static String JAVA_LIBRARY_PATH = "java.library.path";
public static void addToJavaLibraryPath(String dir) {
addToJavaLibraryPath(new File(dir));
}
public static void addToJavaLibraryPath(File dir) {
if (!dir.isDirectory()) {
throw new IllegalArgumentException(dir + " is not a directory.");
}
String javaLibraryPath = System.getProperty(JAVA_LIBRARY_PATH);
System.setProperty(JAVA_LIBRARY_PATH, javaLibraryPath + File.pathSeparatorChar + dir.getAbsolutePath());
resetJavaLibraryPath();
}
private static void resetJavaLibraryPath() {
synchronized (Runtime.getRuntime()) {
try {
Field field = ClassLoader.class.getDeclaredField("usr_paths");
field.setAccessible(true);
field.set(null, null);
field = ClassLoader.class.getDeclaredField("sys_paths");
field.setAccessible(true);
field.set(null, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
} |