Skip to content

Instantly share code, notes, and snippets.

@domdorn
Created March 13, 2021 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domdorn/99b7f4d358b392cbf41ae462a93f97f3 to your computer and use it in GitHub Desktop.
Save domdorn/99b7f4d358b392cbf41ae462a93f97f3 to your computer and use it in GitHub Desktop.
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 = oldPath.map(currentPath => if(!currentPath.contains(pathToAdd))
pathToAdd + ":" + currentPath
else
currentPath
)
.getOrElse(pathToAdd)
// set the system property
System.setProperty("java.library.path", newPath)
// force the refresh
val field = classOf[ClassLoader].getDeclaredField("sys_paths")
field.setAccessible(true)
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