created by aha00a at 2012-07-28
last modified by aha00a at 2012-07-28
revision: 2

Blame of JavaMinusDJavaLibraryPathUsingCode

older newer
DateTime Author Rev # Content Comment
2012-07-28T16:31:18 aha00a 1 0
2012-07-28T16:31:18 aha00a 1 1 = [wiki:Java] `-Djava.library.path` Using Code =
2012-07-28T16:31:18 aha00a 1 2
2012-07-28T16:31:18 aha00a 1 3 `[wiki:JavaMinusDJavaLibraryPathUsingCode Java -Djava.library.path Using Code]`
2012-07-28T16:31:18 aha00a 1 4
2012-07-28T16:31:18 aha00a 1 5 [wiki:Java]의 VM option중 `-Djava.library.path`를 code로 설정하는 방법
2012-07-28T16:31:18 aha00a 1 6
2012-07-28T16:31:18 aha00a 1 7 [[[
2012-07-28T16:31:18 aha00a 1 8 #!Vim
2012-07-28T16:31:18 aha00a 1 9 #!java
2012-07-28T16:31:18 aha00a 1 10 public class JavaLibraryPathUtil {
2012-07-28T16:31:18 aha00a 1 11 public static void addDir(String dir) throws IOException {
2012-07-28T16:31:18 aha00a 1 12 try {
2012-07-28T16:31:18 aha00a 1 13 Field field = ClassLoader.class.getDeclaredField("usr_paths");
2012-07-28T16:31:18 aha00a 1 14 field.setAccessible(true);
2012-07-28T16:31:18 aha00a 1 15 String[] paths = (String[])field.get(null);
2012-07-28T16:31:18 aha00a 1 16 for (int i = 0; i < paths.length; i++) {
2012-07-28T16:31:18 aha00a 1 17 if (dir.equals(paths[i])) {
2012-07-28T16:31:18 aha00a 1 18 return;
2012-07-28T16:31:18 aha00a 1 19 }
2012-07-28T16:31:18 aha00a 1 20 }
2012-07-28T16:31:18 aha00a 1 21 String[] tmp = new String[paths.length+1];
2012-07-28T16:31:18 aha00a 1 22 System.arraycopy(paths, 0, tmp, 0, paths.length);
2012-07-28T16:31:18 aha00a 1 23 tmp[paths.length] = dir;
2012-07-28T16:31:18 aha00a 1 24 field.set(null,tmp);
2012-07-28T16:31:18 aha00a 1 25 System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + dir);
2012-07-28T16:31:18 aha00a 1 26 } catch (IllegalAccessException e) {
2012-07-28T16:31:18 aha00a 1 27 throw new IOException("Failed to get permissions to set library path");
2012-07-28T16:31:18 aha00a 1 28 } catch (NoSuchFieldException e) {
2012-07-28T16:31:18 aha00a 1 29 throw new IOException("Failed to get field handle to set library path");
2012-07-28T16:31:18 aha00a 1 30 }
2012-07-28T16:31:18 aha00a 1 31 }
2012-07-28T16:31:18 aha00a 1 32
2012-07-28T16:31:18 aha00a 1 33 }
2012-07-28T16:31:18 aha00a 1 34 ]]]
2012-07-28T16:33:48 aha00a 2 35
2012-07-28T16:33:48 aha00a 2 36 == References ==
2012-07-28T16:33:48 aha00a 2 37 * http://stackoverflow.com/questions/5419039/is-djava-library-path-equivalent-to-system-setpropertyjava-library-path