Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Djava.library.path programmatically (or alternatives)?

Tags:

java

jvm

I am looking to set the VM argument Djava.library.path programmatically. If this can't be done, what are the alternatives (if there are any)?

like image 584
Jire Avatar asked Apr 12 '13 01:04

Jire


2 Answers

The solution is easy with this method:

public static void addLibraryPath(String pathToAdd) throws Exception {
    Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathsField.setAccessible(true);

    String[] paths = (String[]) usrPathsField.get(null);

    for (String path : paths)
        if (path.equals(pathToAdd))
            return;

    String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
    newPaths[newPaths.length - 1] = pathToAdd;
    usrPathsField.set(null, newPaths);
}
like image 59
Jire Avatar answered Oct 04 '22 20:10

Jire


take a look at this java doc http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#setProperty(java.lang.String, java.lang.String)

you want to call the setProperty(String, String) method.

so it would look something like this in your case

System.setProperty("java.library.path","value_you_want");
like image 41
Connr Avatar answered Oct 04 '22 18:10

Connr