Skip to content

Instantly share code, notes, and snippets.

@yashk
Created March 25, 2016 02:27
Show Gist options
  • Save yashk/68e910f289ca39704884 to your computer and use it in GitHub Desktop.
Save yashk/68e910f289ca39704884 to your computer and use it in GitHub Desktop.
FindJarForClass
import java.net.URL;
public class FindJarForClass {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(findPathJar(Class.forName(args[0])));
}
public static String findPathJar(Class<?> context) throws IllegalStateException {
URL location = context.getResource('/' + context.getName().replace(".", "/")
+ ".class");
String jarPath = location.getPath();
return jarPath.substring("file:".length(), jarPath.lastIndexOf("!"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment