Created
March 13, 2021 17:18
-
-
Save domdorn/99b7f4d358b392cbf41ae462a93f97f3 to your computer and use it in GitHub Desktop.
Adjust Java Library Path at Runtime (with Scala)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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