Java Utililty Methods Class Path

List of utility methods to do Class Path

Description

The list of methods to do Class Path are organized into topic(s).

Method

StringgetAbsolutePath(Class theClass, String path)
Finds the absolute path to a file from the classpath
URL url = theClass.getClassLoader().getResource(path);
if (url == null) {
    throw new NullPointerException("the file " + path + " does not exist in the classpath");
return URLDecoder.decode(url.getFile());
StringgetAbsolutePath(final Class clazz, final String relative)
get Absolute Path
URL url = clazz.getResource(relative);
String absolutePath = url.getFile().replaceAll("^/+", "");
return absolutePath;
StringgetApplicationPath(Class cls)
get Application Path
if (cls == null)
    throw new java.lang.IllegalArgumentException("parameter is not null !");
ClassLoader loader = cls.getClassLoader();
String clsName = cls.getName() + ".class";
Package pack = cls.getPackage();
System.out.println("package name is : " + (pack == null));
String path = "";
if (pack != null) {
...
FilegetAppPath(Class clazz)
get App Path
File appPath = new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
return appPath.isFile() ? appPath.getParentFile() : appPath;
ClassgetClass(String parentPath, String className)
get Class
File file = new File(parentPath);
URI uri = file.toURI();
URL url = uri.toURL();
URL[] urls = new URL[] { url };
ClassLoader loader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
Class cls = loader.loadClass(className);
return cls;
StringgetClassAbsolutePath(Class c)
get Class Absolute Path
String filePath = c.getProtectionDomain().getCodeSource().getLocation().getFile();
try {
    return URLDecoder.decode(filePath, "UTF-8");
} catch (UnsupportedEncodingException e) {
    return null;
FilegetClassBasePath(Class aClass)
get Class Base Path
String className = aClass.getName();
className = className.replace('.', '/') + ".class";
URL url = aClass.getClassLoader().getResource(className);
if ("file".equals(url.getProtocol())) {
    String dir = url.getFile();
    dir = dir.substring(0, dir.length() - className.length());
    try {
        dir = URLDecoder.decode(dir, "UTF-8");
...
StringgetClassFilePath(Class clazz)
get Class File Path
try {
    return java.net.URLDecoder.decode(getClassFile(clazz).getAbsolutePath(), "UTF-8");
} catch (Exception e) {
    e.printStackTrace();
    return "";
StringgetClassFilePath(Class clazz)
get Class File Path
java.net.URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
String filePath = null;
try {
    filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
} catch (UnsupportedEncodingException ignore) {
File file = new File(filePath);
return file.getAbsolutePath();
...
StringgetClassFilePath(Class clazz)
Gets the path to a class file
String className = clazz.getName();
String resourceName = "/" + className.replace('.', '/') + ".class";
URL classURL = clazz.getResource(resourceName);
if (classURL != null) {
    try {
        if (classURL.toString().contains("bundle")) {
            classURL = FileLocator.resolve(classURL);
        File classFile = new File(classURL.getFile());
        String classPathBase = classFile.getAbsolutePath();
        return classPathBase;
    } catch (Exception e) {
        e.printStackTrace();
return className;