17

Can I set java.library.path programmatically from java code itself?

The following doesn't work.

    System.setProperty("java.library.path", "/blah");
2
  • If it doesn't work, why do you ask? Commented Aug 2, 2011 at 9:04
  • 3
    I meant the snippet I gave doesn't work. Commented Aug 2, 2011 at 9:07

5 Answers 5

23

Maybe this will help: Setting "java.library.path" programmatically

When messing around with JNI, one has to set the java.library.path accordingly. Unfortunately the only way is to add a system property before the application is started:

java -Djava.library.path=/path/to/libs  

Changing the system property later doesn’t have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java…

System.setProperty( "java.library.path", "/path/to/libs" );  
Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );  
fieldSysPath.setAccessible( true );  
fieldSysPath.set( null, null );  

Explanation

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically.

The Classloader has a static field (sys_paths) that contains the paths. If that field is set to null, it is initialized automatically. Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called…

Sign up to request clarification or add additional context in comments.

6 Comments

+1 - Very cool hack - I will try this as the last resort [this is very cool, but horrible for maintainability!].
Please quote the relevant parts of the article in your answer as links can go dead (which yours has).
You should be really careful with this, not only is it implementation based, but it might also cause unexpected exceptions when a concurrent thread tries to load a native library and has just passed the null check for this field when you set it to null
@secmask what if I have this code wrapped in a jar? How do I find out my "/path/to/libs" ?
Unfortunately, this does not work anymore with JDK21, compiling error: SEVERE: null, java.lang.NoSuchFieldException: sys_paths
|
19

No you can't. This property is a read only value. You can change it at JVM launchin time with:

-Djava.library.path=your_path

If you want to load a library from a specific location, you can use System.load(libraryPath) instead with the full path to the library.

1 Comment

I wanted to accept both the answers, but as I can accept only one I will go with this,as the cool hack relies on implementation details.
0

I'm just quoting from the link provided by secmask (https://cedarsoft.com/blog.html), in case the link goes dead:

Changing the system property java.library.path later doesn’t have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java.

System.setProperty("java.library.path", "/path/to/libs");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);

Explanation:

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically. The Classloader has a static field (sys_paths) that contains the paths. If that field is set to null, it is initialized automatically.Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called.

1 Comment

Unfortunately, this does not work anymore with JDK21, compiling error: SEVERE: null, java.lang.NoSuchFieldException: sys_paths
-1

Yes it will read the Environment Variables. Following is the code for setting the Environment variable using the ini4j.

import java.io.IOException;
import org.ini4j.Reg;


public class SettingWinEnvironmentUsing_ini4j {

    public static void main(String args[])
    {
        System.out.println("Setting System Environment Variables");

        Reg reg = new Reg();
        Reg.Key env = reg.add("HKEY_CURRENT_USER\\Environment");
        env.put("RR_PROPERTY_PATH", "c:\\path");
        try {
            reg.write();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(env.get("RR_PROPERTY_PATH"));
    }
}

You can find the ini4j jar at

http://cropforge.org/plugins/scmsvn/viewcvs.php/IAPlugin/lib/ini4j-0.5.2-SNAPSHOT.jar?rev=656&root=icisjavatools&view=log

Comments

-6
import java.util.Map;

public class ReadingEnvironment {

    public static void main(String[] args) {
        System.out.println("Reading System Environment Variables:\n");
        // System.out.println(System.getenv());
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }
    }
}

1 Comment

Does this answer the question at all? It prints out a list of the variables, it doesn't set any.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.