Created
March 25, 2016 02:27
-
-
Save yashk/68e910f289ca39704884 to your computer and use it in GitHub Desktop.
FindJarForClass
This file contains 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
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