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

StringgetClassPath(Class clazz)
get Class Path
String name = (new StringBuilder()).append('/').append(clazz.getName().replace('.', '/')).append(".class")
        .toString();
String osName = name.replace('/', File.separator.charAt(0));
URL resource = clazz.getResource(name);
String absolutePath = (new File(resource.toURI())).getAbsolutePath();
return absolutePath.substring(0, absolutePath.indexOf(osName));
ListgetClasspath(ClassLoader classLoader)
get Classpath
List<URL> implementationClassPath = new ArrayList<URL>();
ClassLoader stopAt = ClassLoader.getSystemClassLoader() == null ? null
        : ClassLoader.getSystemClassLoader().getParent();
for (ClassLoader cl = classLoader; cl != null && cl != stopAt; cl = cl.getParent()) {
    if (cl instanceof URLClassLoader) {
        implementationClassPath.addAll(Arrays.asList(((URLClassLoader) cl).getURLs()));
return implementationClassPath;
StringgetClassPath(ClassLoader loader)
get Class Path
StringBuilder classPath = new StringBuilder();
if (loader instanceof URLClassLoader) {
    URLClassLoader urlLoader = (URLClassLoader) loader;
    for (URL url : urlLoader.getURLs()) {
        String file = url.getFile();
        if (file.indexOf("junit") == -1) {
            classPath.append(url.toURI().getPort());
            classPath.append(System.getProperty("path.separator"));
...
StringgetClasspath(ClassLoader loader)
get Classpath
Set<URL> jars = getClassLoaderClasspath(loader);
return buildClasspath(jars);
URL[]getClassPath(File dir)
get Class Path
assert dir.isDirectory();
try {
    File[] files = dir.listFiles();
    URL[] urls = new URL[files.length];
    for (int i = 0; i < files.length; i++) {
        urls[i] = files[i].toURL();
    return urls;
...
StringgetClassPath(Object obj, boolean bOnlyPath)
get Class Path
String path = null;
if (obj != null) {
    String className = obj.getClass().getName();
    int index = className.lastIndexOf(".");
    if (index >= 0)
        className = className.substring(index + 1, className.length());
    URL url = obj.getClass().getResource(className + ".class");
    if (url != null) {
...
FilegetClassPath(String packageName, String name)
get Class Path
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
try {
    path = URLDecoder.decode(path, ENCODING);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
File file = new File(path + getFilePath(packageName, name));
return file;
...
StringgetClassPathAsString(ClassLoader classLoader)
get Class Path As String
String path = "";
try {
    URL[] urls = ((URLClassLoader) classLoader).getURLs();
    for (int i = 0; i < urls.length; i++) {
        if (path != "") {
            path += ";";
        path += urls[i].toString();
...
StringgetClasspathDir(Class klass)
get Classpath Dir
String file = klass.getName();
file = file.replace('.', '/') + ".class";
URL url = Thread.currentThread().getContextClassLoader().getResource(file);
String baseDir = url.toURI().getPath();
baseDir = baseDir.substring(0, baseDir.length() - file.length() - 1);
return baseDir;
FilegetClasspathFile(Class clz)
Return the file that forms the classpath for the given class.
String className = clz.getName();
String baseName = className.substring(className.lastIndexOf(".") + 1);
URL classUrl = clz.getResource(baseName + ".class");
if (classUrl == null)
    return null;
String classUrlStr = classUrl.toString();
if (classUrlStr.startsWith("jar:file:"))
    return getFileForUrl(classUrlStr);
...