Java ClassPath Get getClassPathFor(final Class clazz)

Here you can find the source of getClassPathFor(final Class clazz)

Description

Returns the path for the jar or directory that contains a given java class.

License

Open Source License

Parameter

Parameter Description
clazz the class whose classpath entry needs to be located.

Return

an url with the jar or directory that contains that class

Declaration

public static URL getClassPathFor(final Class<?> clazz) 

Method Source Code


//package com.java2s;
import java.io.File;

import java.net.URL;

public class Main {
    /**//from  w  ww .  ja  va  2 s  .  c  o  m
     * Returns the path for the jar or directory that contains a given java class.
     *
     * @param clazz the class whose classpath entry needs to be located.
     * @return an url with the jar or directory that contains that class
     */
    public static URL getClassPathFor(final Class<?> clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("Null class");
        }
        try {
            final URL url = getClassFile(clazz);
            String urlString = url.toString();
            final int endIndex = urlString.toLowerCase().indexOf("!");
            if (endIndex > 0) {
                // assuming it's something inside a jar
                int beginIndex = urlString.lastIndexOf("file:/");
                if (beginIndex >= 0) {
                    return new URL(urlString.substring(beginIndex, endIndex));
                }
                beginIndex = urlString.lastIndexOf("[a-zA-Z]+://");
                if (beginIndex > 0) {
                    return new URL(urlString.substring(beginIndex, endIndex));
                }
            } else {
                // assuming it's a file
                File dir = new File(url.toURI()).getParentFile();
                if (clazz.getPackage() != null) {
                    String pn = clazz.getPackage().getName();
                    for (int i = pn.indexOf('.'); i >= 0; i = pn.indexOf('.', i + 1)) {
                        dir = dir.getParentFile();
                    }
                    dir = dir.getParentFile();
                }
                return dir.toURI().toURL();
            }
            throw new RuntimeException("Error locating classpath entry for: " + clazz.getName() + " url: " + url);
        } catch (Exception e) {
            throw new RuntimeException("Error locating classpath entry for: " + clazz.getName(), e);
        }
    }

    /**
     * Get the actual classpath resource location for a class
     *
     * @param clazz the class whose .class file must be located.
     * @return a resource url or null if the class is runtime generated.
     */
    public static URL getClassFile(final Class<?> clazz) {
        int idx = clazz.getName().lastIndexOf('.');
        final String fileName = (idx >= 0 ? clazz.getName().substring(idx + 1) : clazz.getName()) + ".class";
        return clazz.getResource(fileName);
    }
}

Related

  1. getClasspathFile(final String name)
  2. getClassPathFile(final String path, final ClassLoader classLoader)
  3. getClasspathFile(String fn)
  4. getClassPathFile(String pathName)
  5. getClasspathFilePathFromName(String fileName)
  6. getClasspathForClass(Class targetClass)
  7. getClassPathFromString(String classpath)
  8. getClassPathPath()
  9. getClasspathResource(final String resource)