Last active
August 27, 2020 09:39
-
-
Save prakharjain09/9286848047b33d7140de370097a3d311 to your computer and use it in GitHub Desktop.
Scala Get All jars containing a particular class in classpath
This file contains hidden or 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
| 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