Skip to content

Instantly share code, notes, and snippets.

@passionne
Created August 6, 2015 14:10
Show Gist options
  • Save passionne/ff5a3db472267f720b54 to your computer and use it in GitHub Desktop.
Save passionne/ff5a3db472267f720b54 to your computer and use it in GitHub Desktop.
update java library path programmatically
#!/bin/bash
# Inspired by : http://blog.cedarsoft.com/2010/11/setting-java-library-path-programmatically/
function testJavaVersion() {
version=$1
echo "java ${version}"
docker run --rm -t -v "$(pwd):/opt" \
java:${version} \
/bin/bash -c "cd /opt && javac UpdateJavaLibPath.java && java UpdateJavaLibPath && rm UpdateJavaLibPath.class"
}
for version in 6 7 8
do
testJavaVersion $version
done
import java.lang.ClassLoader;
import java.lang.String;
import java.lang.System;
import java.lang.reflect.Field;
class UpdateJavaLibPath {
public static String printLibPath() {
String current = System.getProperty("java.library.path");
System.out.println(String.format("current = %s", current));
return current;
}
public static void main(String[] args) {
String current = printLibPath();
System.setProperty( "java.library.path", current + ":/tmp" );
try {
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );
printLibPath();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment