Reputation:
Can I set java.library.path programmatically from java code itself?
The following doesn't work.
System.setProperty("java.library.path", "/blah");
Upvotes: 17
Views: 15580
Reputation: 8107
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 );
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…
Upvotes: 23
Reputation: 520
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 tonull
, it is initialized automatically.Therefore forcing that field tonull
will result into the reevaluation of the library path as soon asloadLibrary()
is called.
Upvotes: 0
Reputation: 29
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
Upvotes: -1
Reputation: 29
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));
}
}
}
Upvotes: -6
Reputation: 19050
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.
Upvotes: 19