Java ClassPath Get getClasspathForClass(Class targetClass)

Here you can find the source of getClasspathForClass(Class targetClass)

Description

get Classpath For Class

License

Apache License

Declaration

public static File getClasspathForClass(Class<?> targetClass) 

Method Source Code


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

import java.io.File;

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static File getClasspathForClass(Class<?> targetClass) {
        URI location = null;/* w  w  w  .j  a  va 2s .  co m*/
        try {
            location = targetClass.getProtectionDomain().getCodeSource().getLocation().toURI();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        if (!location.getScheme().equals("file")) {
            throw new RuntimeException(String.format("Cannot determine classpath for %s from codebase '%s'.",
                    targetClass.getName(), location));
        }
        return new File(location.getPath());
    }
}

Related

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