Java Class Path getClassFilePath(final Class cls)

Here you can find the source of getClassFilePath(final Class cls)

Description

Get the path of the class file for a given class.

License

Open Source License

Parameter

Parameter Description
cls the class to get the path for

Return

the path of this class file or the path of the JAR file this class file resides in, whatever applies

Declaration

public static String getClassFilePath(final Class cls) 

Method Source Code

//package com.java2s;
/*//from ww w  . j  a va2  s .  co  m
 * SimplyHTML, a word processor based on Java, HTML and CSS
 * Copyright (C) 2002 Ulrich Hilger
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.net.URL;

public class Main {
    public static final String FILE_PREFIX = "file:";
    public static final String CLASS_EXT = ".class";
    public static final String JAR_SEPARATOR = "!/";
    public static final String URL_SEPARATOR = "/";
    public static final String CLASS_SEPARATOR = ".";

    /**
     * Get the path of the class file for a given class.
     *
     * <p>This is either a directory of a class file or a directory of
     * a JAR file. Thus, this class must reside in the same place as
     * the application in question, not in a separate library
     * for instance.</p>
     *
     * @param cls  the class to get the path for
     *
     * @return the path of this class file or the path of the JAR file this class
     *      file resides in, whatever applies
     */
    public static String getClassFilePath(final Class cls) {
        int end = 0;
        String urlStr = null;
        String clsName = cls.getName();
        final int clsNameLen = clsName.length() + CLASS_EXT.length();
        int pos = clsName.lastIndexOf(CLASS_SEPARATOR);
        if (pos > -1) {
            clsName = clsName.substring(pos + 1);
        }
        clsName = clsName + CLASS_EXT;
        final URL url = cls.getResource(clsName);
        if (url != null) {
            urlStr = url.toString();
            pos = urlStr.indexOf(JAR_SEPARATOR);
            if (pos > -1) {
                urlStr = urlStr.substring(0, pos);
                end = urlStr.lastIndexOf(URL_SEPARATOR) + 1;
            } else {
                end = urlStr.length() - clsNameLen;
            }
            pos = urlStr.lastIndexOf(FILE_PREFIX);
            if (pos > -1) {
                pos += FILE_PREFIX.length() + 1;
            } else {
                pos = 0;
            }
            urlStr = urlStr.substring(pos, end);
            urlStr = urlStr.replaceAll("%20", " ");
        }
        return urlStr;
    }
}

Related

  1. getClassAbsolutePath(Class c)
  2. getClassBasePath(Class aClass)
  3. getClassFilePath(Class clazz)
  4. getClassFilePath(Class clazz)
  5. getClassFilePath(Class clazz)
  6. getClassFilePath(String package_name, String class_name)
  7. getClassNameByFile(String filePath, List className, boolean childPackage)
  8. getDirectoryPath(Class owner)
  9. getFile(Class base, String path)