Skip to content
Snippets Groups Projects
Commit 0aa23bf1 authored by Matei Zaharia's avatar Matei Zaharia
Browse files

Added a convenience method for getting the JAR file that loaded a class

(useful for jobs to pass their own JAR files to SparkContext).
parent 9b7215d7
No related branches found
No related tags found
No related merge requests found
......@@ -353,6 +353,24 @@ object SparkContext {
implicit def writableWritableConverter[T <: Writable]() =
new WritableConverter[T](_.erasure.asInstanceOf[Class[T]], _.asInstanceOf[T])
// Find the JAR from which a given class was loaded, to make it easy for users to pass
// their JARs to SparkContext
def jarOfClass[T: ClassManifest]: Option[String] = {
val cls = classManifest[T].erasure
val uri = cls.getResource("/" + cls.getName.replace('.', '/') + ".class")
if (uri != null) {
val uriStr = uri.toString
if (uriStr.startsWith("jar:file:")) {
// URI will be of the form "jar:file:/path/foo.jar!/package/cls.class", so pull out the /path/foo.jar
Some(uriStr.substring("jar:file:".length, uriStr.indexOf('!')))
} else {
None
}
} else {
None
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment