Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.io.File;
- import java.io.IOException;
- import java.util.Enumeration;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- import test.classtest.TestClass;
- public class TestArea {
- public static void main (String args[]) throws IOException {
- ClassLoader loader = TestClass.class.getClassLoader();
- // find jar file of a class
- // method 1
- // ref http://stackoverflow.com/questions/1983839/determine-which-jar-file-a-class-is-from
- String name = TestClass.class.getName().replaceAll("\\.", "/") + ".class";
- System.out.println(loader.getResource(name));
- // method 2
- // ref http://stackoverflow.com/questions/4546632/how-to-determine-absolute-path-if-any-of-a-loaded-class
- String path = TestClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
- System.out.println(path);
- // find jars contain TestClass.class
- // ref http://stackoverflow.com/questions/9630268/read-contents-of-jar-file-given-specific-path
- File f = new File(path).getParentFile(); // WEB-INF/lib
- for (File jar : f.listFiles()) {
- String jarPath = jar.getAbsolutePath();
- JarFile jarFile = new JarFile(jarPath);
- for(Enumeration<JarEntry> em = jarFile.entries(); em.hasMoreElements();) {
- String s= em.nextElement().toString();
- if (s.endsWith("/TestClass.class")) {
- System.out.println(jarPath + " contains TestClass.class");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment