Here you can find the source of findJarContaining(Class> c)
static JarFile findJarContaining(Class<?> c) throws ClassNotFoundException, IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.jar.JarFile; public class Main { static JarFile findJarContaining(Class<?> c) throws ClassNotFoundException, IOException { URLClassLoader cl = (URLClassLoader) c.getClassLoader(); String cn = c.getCanonicalName(); URL url = cl.findResource(cn.replace(".", "/") + ".class"); String s = url.getFile().substring(url.getProtocol().length() + 2); String jar = s.substring(0, s.lastIndexOf("!")); return new JarFile(jar); }//w w w. ja v a 2 s . c o m public static JarFile findJarContaining(String cn) throws ClassNotFoundException, IOException { return findJarContaining(Class.forName(cn)); } }