Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save prakharjain09/9286848047b33d7140de370097a3d311 to your computer and use it in GitHub Desktop.

Select an option

Save prakharjain09/9286848047b33d7140de370097a3d311 to your computer and use it in GitHub Desktop.
Scala Get All jars containing a particular class in classpath
val inputClassName = "" // Type input class name which you want to find. Example - org/apache/hadoop/fs/s3a/S3AFileSystem.class
import java.net.URLClassLoader
import java.net.URL
import java.io.File
def list_urls(cl: ClassLoader): Array[java.net.URL] = cl match {
case null => Array()
case u: java.net.URLClassLoader => u.getURLs() ++ list_urls(cl.getParent)
case _ => list_urls(cl.getParent)
}
def findJarsHavingClass(name: String, jarList: Array[URL]): Array[URL] = {
var resultJarsArray = Array[URL]()
for( x <- jarList ){
val ucl = new URLClassLoader(Array(x), ClassLoader.getSystemClassLoader.getParent.getParent);
val classPath = ucl.findResource(name)
if (classPath != null) {
resultJarsArray = resultJarsArray :+ classPath
}
}
return resultJarsArray
}
val allJars = list_urls(getClass.getClassLoader).distinct
val requiredJars = findJarsHavingClass(inputClassName, allJars)
requiredJars.foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment