Java Utililty Methods Class Path

List of utility methods to do Class Path

Description

The list of methods to do Class Path are organized into topic(s).

Method

StringgetClassFilePath(final Class cls)
Get the path of the class file for a given class.
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;
StringgetClassFilePath(String package_name, String class_name)
get Class File Path
String path = null;
try {
    Class c = Class.forName(package_name + "." + class_name);
    path = getClassFilePath(c);
} catch (ClassNotFoundException e) {
    System.err.println("Could not locate class " + package_name + "." + class_name);
    path = null;
return path;
ListgetClassNameByFile(String filePath, List className, boolean childPackage)
get Class Name By File
List<String> myClassName = new ArrayList<String>();
filePath = URLDecoder.decode(filePath, "utf-8");
File file = new File(filePath);
File[] childFiles = file.listFiles();
for (File childFile : childFiles) {
    if (childFile.isDirectory()) {
        if (childPackage) {
            myClassName.addAll(getClassNameByFile(childFile.getPath(), myClassName, childPackage));
...
StringgetDirectoryPath(Class owner)
Get directory path according to Class.
String packageName = owner.getPackage().getName().replace(".", String.valueOf(File.separator));
URL resource = owner.getClassLoader().getResource(packageName);
if (resource != null) {
    return resource.getPath();
return null;
FilegetFile(Class base, String path)
get File
return urlToFile(base.getResource(path));
StringgetFullPathOfClass(Class cls)
get Full Path Of Class
URL url = cls.getProtectionDomain().getCodeSource().getLocation();
String path = url.getPath();
String[] dirs = cls.getName().split("\\.");
for (int i = 0; i < dirs.length - 1; i++)
    path += dirs[i] + "/";
return path;
StringgetFullpathRessources(Class c, String ressource)
get Fullpath Ressources
URL url = c.getResource(ressource);
if (url == null) {
    return null;
try {
    Class<?> fl = Class.forName("org.eclipse.core.runtime.FileLocator");
    url = (URL) fl.getMethod("toFileURL", URL.class).invoke(null, url);
} catch (Exception e) {
...
InputStreamgetInputStream(Class cls, String path)
This method attempts to create an input stream from the given path by:
  1. Trying to parse it as a URL
  2. Trying to find it on the classpath, relative to the given class
  3. Trying to find it as a file on the file system
InputStream inputStream;
try {
    inputStream = new URL(path).openStream();
} catch (MalformedURLException e) {
    inputStream = cls.getResourceAsStream(path);
    if (inputStream == null) {
        try {
            inputStream = new FileInputStream(path);
...
StringgetPath(Class tClass)
get Path
String rtn = null;
URL url = getURL(tClass);
if (url != null) {
    rtn = url.getPath();
    rtn = rtn.replace(tClass.getSimpleName() + ".class", "");
return rtn;
StringgetPath(final String aPath, final Class aClass)
get Path
final URL url = getClasspathUrl(aClass, aPath);
return url != null ? url.toString() : aPath;