Java ClassPath Get getClassPathFile(Class clazz)

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

Description

Gets the class path file.

License

Open Source License

Parameter

Parameter Description
clazz the clazz

Return

the class path file

Declaration

public static File getClassPathFile(Class<?> clazz) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.net.URL;

public class Main {
    /**//from   w  w  w.  j a  v  a 2s .  com
     * Gets the class path file.
     *
     * @param clazz
     *            the clazz
     * @return the class path file
     */
    public static File getClassPathFile(Class<?> clazz) {
        File file = getClassFile(clazz);
        for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++)
            file = file.getParentFile();
        if (file.getName().toUpperCase().endsWith(".JAR!")) {
            file = file.getParentFile();
        }
        return file;
    }

    /**
     * Gets the class file.
     *
     * @param clazz
     *            the clazz
     * @return the class file
     */
    public static File getClassFile(Class<?> clazz) {
        URL path = clazz.getResource(clazz.getName().substring(clazz.getName().lastIndexOf(".") + 1) + ".classs");
        if (path == null) {
            String name = clazz.getName().replaceAll("[.]", "/");
            path = clazz.getResource("/" + name + ".class");
        }
        return new File(path.getFile());
    }
}

Related

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