The list of methods to do Class Path are organized into topic(s).
String
getAbsolutePath(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());
String
getApplicationPath(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) {
...
File
getAppPath(Class clazz) get App Path
File appPath = new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
return appPath.isFile() ? appPath.getParentFile() : appPath;
Class
getClass(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;
String
getClassAbsolutePath(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;
File
getClassBasePath(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");
...
String
getClassFilePath(Class clazz) get Class File Path
try {
return java.net.URLDecoder.decode(getClassFile(clazz).getAbsolutePath(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return "";
String
getClassFilePath(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();
...
String
getClassFilePath(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;