Java Class Loader getClassLocation(Class clazz)

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

Description

Returns the location of the given class.

License

Apache License

Parameter

Parameter Description
clazz The class to get its location.

Return

A non null with the location of the given class.

Declaration

public static String getClassLocation(Class<?> clazz) 

Method Source Code

//package com.java2s;
/**/*ww  w.  ja va 2 s.c  o m*/
 * Copyright 2013 Contributors
 *
 *    Licensed 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.
 *
 *    Contributors:
 *          Alessandro Ferreira Leite - the initial implementation.
 */

import java.io.File;

import java.net.URL;

public class Main {
    /**
     * Returns the location of the given class.
     * 
     * @param clazz
     *            The class to get its location.
     * @return A non null {@link String} with the location of the given class.
     */
    public static String getClassLocation(Class<?> clazz) {
        String className = "/" + clazz.getName().replace('.', '/') + ".class";
        URL u = clazz.getResource(className);
        String s = u.toString();

        if (s.startsWith("jar:file:/")) {
            int pos = s.indexOf(".jar!/");
            if (pos != -1) {
                if (File.separator.equals("\\")) {
                    s = s.substring("jar:file:/".length(), pos + ".jar".length());
                } else {
                    s = s.substring("jar:file:".length(), pos + ".jar".length());
                }
                s = s.replaceAll("%20", " ");
            } else {
                s = "?";
            }
        } else if (s.startsWith("file:/")) {
            if (File.separator.equals("\\")) {
                s = s.substring("file:/".length());
            } else {
                s = s.substring("file:".length());
            }
            s = s.substring(0, s.lastIndexOf(className)).replaceAll("%20", " ");
        } else {
            s = "?";
        }
        return s;
    }
}

Related

  1. getClassLoaderForClass(final Class clazz)
  2. getClassLoaderForDirectory(final File baseFolder)
  3. getClassloaderRootDir(Class forClass)
  4. getClassLoaders(ClassLoader baseClassLoader)
  5. getClassLoaderStack(ClassLoader cl)
  6. getClassLocation(String className)
  7. getCustomClassloader(List entries)
  8. getFileFromClassLoader(String fileName)
  9. getFilesByPackgeName(String packageName, File _file, ClassLoader classLoader, List list)