Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Adjust Java Library Path at Runtime (with Scala)
object AdjustJavaLibraryPathAtRuntime {
def addJavaLibraryPath(pathToAdd: String): Unit = {
val oldPath = Option(System.getProperty("java.library.path"))
// only add path if its not already included
val newPath = => if(!currentPath.contains(pathToAdd))
pathToAdd + ":" + currentPath
// set the system property
System.setProperty("java.library.path", newPath)
// force the refresh
val field = classOf[ClassLoader].getDeclaredField("sys_paths")
val oldRtPath = field.get(null)
field.set(null, null) // this makes sure the pathes are refreshed
field.set(null, oldRtPath) // make sure to do this, else java.nio will not work anymore!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment