Java Jar File Find findContainingJar( @SuppressWarnings("rawtypes") Class my_class, ClassLoader loader)

Here you can find the source of findContainingJar( @SuppressWarnings("rawtypes") Class my_class, ClassLoader loader)

Description

find Containing Jar

License

Apache License

Declaration

public static String findContainingJar(
            @SuppressWarnings("rawtypes") Class my_class, ClassLoader loader) 

Method Source Code

//package com.java2s;
/**//ww w .j  a va  2 s  .c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.net.URL;
import java.net.URLDecoder;

import java.util.Enumeration;

public class Main {
    public static String findContainingJar(
            @SuppressWarnings("rawtypes") Class my_class, ClassLoader loader) {
        String class_file = my_class.getName().replaceAll("\\.", "/")
                + ".class";
        return findContainingJar(class_file, loader);
    }

    public static String findContainingJar(String fileName,
            ClassLoader loader) {
        try {
            for (@SuppressWarnings("rawtypes")
            Enumeration itr = loader.getResources(fileName); itr
                    .hasMoreElements();) {
                URL url = (URL) itr.nextElement();
                // logger.info("findContainingJar finds url:" + url);
                if ("jar".equals(url.getProtocol())) {
                    String toReturn = url.getPath();
                    if (toReturn.startsWith("file:")) {
                        toReturn = toReturn.substring("file:".length());
                    }
                    toReturn = URLDecoder.decode(toReturn, "UTF-8");
                    return toReturn.replaceAll("!.*$", "");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}

Related

  1. findAllPluginJars0(final File pluginPath)
  2. findClassJar(final Class aClass)
  3. findContainingJar(Class my_class)
  4. findContainingJar(Class clazz)
  5. findContainingJar(Class clazz)
  6. findContainingJar(Class myClass, Map packagedClasses)