在java程式中動態設定java.library.path
阿新 • • 發佈:2018-11-12
轉載自https://blog.csdn.net/sinianliushui/article/details/68061035public static void addLibraryDir(String libraryPath) throws IOException { try { Field field = ClassLoader.class.getDeclaredField("usr_paths"); field.setAccessible(true); String[] paths = (String[]) field.get(null); for (int i = 0; i < paths.length; i++) { if (libraryPath.equals(paths[i])) { return; } } String[] tmp = new String[paths.length + 1]; System.arraycopy(paths, 0, tmp, 0, paths.length); tmp[paths.length] = libraryPath; field.set(null, tmp); } catch (IllegalAccessException e) { throw new IOException( "Failedto get permissions to set library path"); } catch (NoSuchFieldException e) { throw new IOException( "Failedto get field handle to set library path"); } }