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