Java Class Path getClassFilePath(Class clazz)

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

Description

Gets the path to a class file

License

LGPL

Parameter

Parameter Description
clazz a parameter

Declaration

public static String getClassFilePath(Class<?> clazz) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;

public class Main {
    /**//from w ww. j  a  v  a  2  s .  co  m
     * Gets the path to a class file
     * 
     * @param clazz
     * @return
     */
    public static String getClassFilePath(Class<?> clazz) {
        String className = clazz.getName();
        String resourceName = "/" + className.replace('.', '/') + ".class";
        // get a URL so we can turn it into a file
        URL classURL = clazz.getResource(resourceName);
        if (classURL != null) {
            try {
                if (classURL.toString().contains("bundle")) {
                    classURL = FileLocator.resolve(classURL);
                }
                // get the file, so we can get a full path reference
                File classFile = new File(classURL.getFile());
                // now we have the full file path
                String classPathBase = classFile.getAbsolutePath();
                return classPathBase;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return className;
    }
}

Related

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