Java Jar File Find findJarContaining(Class c)

Here you can find the source of findJarContaining(Class c)

Description

find Jar Containing

License

Apache License

Declaration

static JarFile findJarContaining(Class<?> c) throws ClassNotFoundException, IOException 

Method Source Code


//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));
    }
}

Related

  1. findContainingJar(ClassLoader classLoader, String resourceName)
  2. findJar(Class klass)
  3. findJar(Class my_class)
  4. findJar(File dir)
  5. findJar(String nameRegexp)
  6. findJarEntry(String entryName, JarInputStream jarFile)
  7. findJarFiles(String[] classPathLines)
  8. findJars(File folder)
  9. findJars(File libDir)