Java ClassPath Get getClasspathFile(Class clz)

Here you can find the source of getClasspathFile(Class clz)

Description

Return the file that forms the classpath for the given class.

License

Open Source License

Declaration

public static File getClasspathFile(Class clz) 

Method Source Code

//package com.java2s;
// modify it under the terms of the GNU Lesser General Public License

import java.io.File;

import java.io.UnsupportedEncodingException;

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

public class Main {
    /** Return the file that forms the classpath for the given class.
     * //w  w  w .  j ava 2s .  com
     * If the class has been loaded from a local JAR file, this will return a
     * File object pointing to that JAR file.
     * 
     * If the class has been loaded from a local directory, this will return a
     * File object pointing to the directory that forms the base of the
     * classpath.
     * 
     * If a local file cannot be determined for the given class, returns null.
     */
    public static File getClasspathFile(Class clz) {
        String className = clz.getName();
        String baseName = className.substring(className.lastIndexOf(".") + 1);
        URL classUrl = clz.getResource(baseName + ".class");
        if (classUrl == null)
            return null;

        String classUrlStr = classUrl.toString();
        if (classUrlStr.startsWith("jar:file:"))
            return getFileForUrl(classUrlStr);
        else if (classUrlStr.startsWith("file:"))
            return getDirBasedClasspath(classUrlStr, className);
        else
            return null;
    }

    /**
     * Return the File referenced by a "file:" or "jar:file:" URL.
     * 
     * @param url
     *            a URL; may be null
     * @return the File this URL points to if applicable; otherwise null
     * @since 2.1.8
     */
    public static File getFileForUrl(URL url) {
        return (url == null ? null : getFileForUrl(url.toString()));
    }

    /**
     * Return the File referenced by a "file:" or "jar:file:" URL.
     * 
     * @param url
     *            a URL in string form; may be null
     * @return the File this URL points to if applicable; otherwise null
     * @since 2.1.8
     */
    public static File getFileForUrl(String url) {
        if (url == null)
            return null;

        if (url.startsWith("jar:")) {
            int pos = url.indexOf('!');
            url = (pos == -1 ? url.substring(4) : url.substring(4, pos));
        }

        String jarFileName;
        try {
            if (url.startsWith("file:"))
                jarFileName = URLDecoder.decode(url.substring(5), "UTF-8");
            else
                return null;
        } catch (UnsupportedEncodingException e) {
            // can't happen
            return null;
        }
        File jarFile = new File(jarFileName).getAbsoluteFile();
        return jarFile;
    }

    /** Return a classpath for use with an unpackaged class file
     * 
     * @param selfUrlStr the URL of the class file for this class; must be a
     *    file: URL pointing to a .class file in the "bin" directory of a
     *    process dashboard project directory
     * @return the classpath that can be used to launch a dashboard instance.
     *    This classpath will include the effective "bin" directory that
     *    contains this class, and will also include the JAR files in the
     *    "lib" directory of the process dashboard project directory.
     */
    private static File getDirBasedClasspath(String selfUrlStr, String className) {
        // remove initial "file:" and trailing "/net/..." information
        String classFilename = className.replace('.', '/');
        int packagePos = selfUrlStr.indexOf(classFilename);
        if (packagePos == -1)
            return null;
        selfUrlStr = selfUrlStr.substring(5, packagePos);

        File binDir;
        try {
            String path = URLDecoder.decode(selfUrlStr, "UTF-8");
            binDir = new File(path).getAbsoluteFile();
            return binDir;
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getClassPath(File dir)
  2. getClassPath(Object obj, boolean bOnlyPath)
  3. getClassPath(String packageName, String name)
  4. getClassPathAsString(ClassLoader classLoader)
  5. getClasspathDir(Class klass)
  6. getClassPathFile(Class clazz)
  7. getClasspathFile(final String name)
  8. getClassPathFile(final String path, final ClassLoader classLoader)
  9. getClasspathFile(String fn)