Java Utililty Methods ClassPath Get

List of utility methods to do ClassPath Get

Description

The list of methods to do ClassPath Get are organized into topic(s).

Method

FilegetClassPathFile(Class clazz)
Gets the class path file.
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;
FilegetClasspathFile(final String name)
get Classpath File
URL url = ClassLoader.getSystemResource(name);
if (!url.getProtocol().equals("file")) {
    throw new FileNotFoundException("Could not find file named by resource: " + name);
File file = new File(url.getPath());
return file;
FilegetClassPathFile(final String path, final ClassLoader classLoader)
Gets a File from a String that represents file path which is relative to current Class Path
final URL url = classLoader.getResource(path);
if (url == null) {
    return null;
return new File(url.getFile());
FilegetClasspathFile(String fn)
get Classpath File
File f = null;
try {
    f = new File(Thread.currentThread().getContextClassLoader().getResource(fn).toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
return f;
FilegetClassPathFile(String pathName)
get Class Path File
if (pathName.indexOf("%20") != -1)
    pathName = pathName.replaceAll("%20", " ");
File tmp = new File(getResourceURL(pathName).getFile());
return tmp;
StringgetClasspathFilePathFromName(String fileName)
Gets the absolute path to the specified file on the classpath.
URL fileURL = Thread.currentThread().getContextClassLoader().getResource(fileName);
if (fileURL == null) {
    throw new FileNotFoundException("Failed to find file on classpath: " + fileName);
URI fileURI = fileURL.toURI();
File file = new File(fileURI);
return file.getAbsolutePath();
URLgetClassPathFor(final Class clazz)
Returns the path for the jar or directory that contains a given java class.
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) {
...
FilegetClasspathForClass(Class targetClass)
get Classpath For Class
URI location = null;
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'.",
...
HashSetgetClassPathFromString(String classpath)
get Class Path From String
HashSet<URL> cpurls = new HashSet<URL>();
StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
while (st.hasMoreTokens()) {
    try {
        cpurls.add(new File(st.nextToken()).toURI().toURL());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        System.exit(-1);
...
StringgetClassPathPath()
get Class Path Path
String path = webRoot + "WEB-INF" + File.separator + "classes";
path = urlDecoder(path);
return path;