Skip to content

Instantly share code, notes, and snippets.

@ericacm
Created October 12, 2012 22:19
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 ericacm/3881937 to your computer and use it in GitHub Desktop.
Save ericacm/3881937 to your computer and use it in GitHub Desktop.
classloaderJars
val isWindows = {
val osName = System.getProperty("os.name")
val isWin = osName.startsWith("Windows")
println("os.name=" + osName + " isWindows=" + isWin)
isWin
}
def currentJars: Array[String] = {
val classloader = ClassLoader.getSystemClassLoader
var jars = classloader.asInstanceOf[URLClassLoader].getURLs.map(_.getFile).map(URLDecoder.decode(_, "UTF-8"))
if (jars.length == 1 && jars(0).contains("surefirebooter")) {
jars = surefirebooterJars(jars(0))
}
jars = jars.filter(j => !(
j.contains("/jre/lib/") ||
j.contains("idea_rt.jar") ||
j.contains("surefire/surefire-") ||
j.contains("junit_rt.jar")))
if (isWindows) jars = jars.map(_.tail)
jars
}
def surefirebooterJars(surefireJar: String): Array[String] = {
val is = new FileInputStream(surefireJar)
val jarStream = new JarInputStream(is)
val mf = jarStream.getManifest
val mainAttributes = mf.getMainAttributes
val classpath = mainAttributes.getValue("Class-Path")
classpath.replaceAll("file:", "").split(" ").toArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment