List of utility methods to do Class Path
String | getClassFilePath(final Class cls) Get the path of the class file for a given class. int end = 0; String urlStr = null; String clsName = cls.getName(); final int clsNameLen = clsName.length() + CLASS_EXT.length(); int pos = clsName.lastIndexOf(CLASS_SEPARATOR); if (pos > -1) { clsName = clsName.substring(pos + 1); clsName = clsName + CLASS_EXT; final URL url = cls.getResource(clsName); if (url != null) { urlStr = url.toString(); pos = urlStr.indexOf(JAR_SEPARATOR); if (pos > -1) { urlStr = urlStr.substring(0, pos); end = urlStr.lastIndexOf(URL_SEPARATOR) + 1; } else { end = urlStr.length() - clsNameLen; pos = urlStr.lastIndexOf(FILE_PREFIX); if (pos > -1) { pos += FILE_PREFIX.length() + 1; } else { pos = 0; urlStr = urlStr.substring(pos, end); urlStr = urlStr.replaceAll("%20", " "); return urlStr; |
String | getClassFilePath(String package_name, String class_name) get Class File Path String path = null; try { Class c = Class.forName(package_name + "." + class_name); path = getClassFilePath(c); } catch (ClassNotFoundException e) { System.err.println("Could not locate class " + package_name + "." + class_name); path = null; return path; |
List | getClassNameByFile(String filePath, List get Class Name By File List<String> myClassName = new ArrayList<String>(); filePath = URLDecoder.decode(filePath, "utf-8"); File file = new File(filePath); File[] childFiles = file.listFiles(); for (File childFile : childFiles) { if (childFile.isDirectory()) { if (childPackage) { myClassName.addAll(getClassNameByFile(childFile.getPath(), myClassName, childPackage)); ... |
String | getDirectoryPath(Class Get directory path according to Class. String packageName = owner.getPackage().getName().replace(".", String.valueOf(File.separator)); URL resource = owner.getClassLoader().getResource(packageName); if (resource != null) { return resource.getPath(); return null; |
File | getFile(Class base, String path) get File return urlToFile(base.getResource(path));
|
String | getFullPathOfClass(Class cls) get Full Path Of Class URL url = cls.getProtectionDomain().getCodeSource().getLocation(); String path = url.getPath(); String[] dirs = cls.getName().split("\\."); for (int i = 0; i < dirs.length - 1; i++) path += dirs[i] + "/"; return path; |
String | getFullpathRessources(Class> c, String ressource) get Fullpath Ressources URL url = c.getResource(ressource); if (url == null) { return null; try { Class<?> fl = Class.forName("org.eclipse.core.runtime.FileLocator"); url = (URL) fl.getMethod("toFileURL", URL.class).invoke(null, url); } catch (Exception e) { ... |
InputStream | getInputStream(Class> cls, String path) This method attempts to create an input stream from the given path by:
InputStream inputStream; try { inputStream = new URL(path).openStream(); } catch (MalformedURLException e) { inputStream = cls.getResourceAsStream(path); if (inputStream == null) { try { inputStream = new FileInputStream(path); ... |
String | getPath(Class tClass) get Path String rtn = null; URL url = getURL(tClass); if (url != null) { rtn = url.getPath(); rtn = rtn.replace(tClass.getSimpleName() + ".class", ""); return rtn; |
String | getPath(final String aPath, final Class> aClass) get Path final URL url = getClasspathUrl(aClass, aPath); return url != null ? url.toString() : aPath; |