Java Jar File Find findContainingJar(Class clazz)

Here you can find the source of findContainingJar(Class clazz)

Description

find Containing Jar

License

Open Source License

Declaration

public static String findContainingJar(Class<?> clazz) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;

public class Main {

    public static String findContainingJar(Class<?> clazz) {
        ClassLoader loader = clazz.getClassLoader();
        String classFile = clazz.getName().replaceAll("\\.", "/") + ".class";
        try {/*ww w  .j av  a  2 s.  com*/
            for (final Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) {
                final URL url = itr.nextElement();
                if ("jar".equals(url.getProtocol())) {
                    String path = url.getPath();
                    if (path.startsWith("file:")) {
                        path = path.substring("file:".length());
                    }
                    path = URLDecoder.decode(path, "UTF-8");
                    return path.replaceAll("!.*$", "");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}

Related

  1. findAllPluginJars0(final File pluginPath)
  2. findClassJar(final Class aClass)
  3. findContainingJar( @SuppressWarnings("rawtypes") Class my_class, ClassLoader loader)
  4. findContainingJar(Class my_class)
  5. findContainingJar(Class clazz)
  6. findContainingJar(Class myClass, Map packagedClasses)
  7. findContainingJar(ClassLoader classLoader, String resourceName)
  8. findJar(Class klass)
  9. findJar(Class my_class)