Java Class Path getProgramRootPath(Class clazz)

Here you can find the source of getProgramRootPath(Class clazz)

Description

get Program Root Path

License

Open Source License

Declaration

public static String getProgramRootPath(Class clazz) 

Method Source Code


//package com.java2s;
import java.io.File;
import java.net.URL;

public class Main {

    public static String getProgramRootPath(Class clazz) {
        try {/*from   w  ww  . ja  va 2 s .  c om*/
            return java.net.URLDecoder
                    .decode(getClassPathFile(clazz).getParentFile().getParentFile().getAbsolutePath(), "UTF-8");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return "";
        }
    }

    public static File getClassPathFile(Class clazz) {
        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;
    }

    public static File getClassFile(Class clazz) {
        URL path = clazz.getResource(clazz.getName().substring(clazz.getName().lastIndexOf(".") + 1) + ".classs");
        if (path == null) {
            String name = clazz.getName().replaceAll("[.]", "/");
            path = clazz.getResource("/" + name + ".class");
        }
        return new File(path.getFile());
    }
}

Related

  1. getFullpathRessources(Class c, String ressource)
  2. getInputStream(Class cls, String path)
  3. getPath(Class tClass)
  4. getPath(final String aPath, final Class aClass)
  5. getPath(String name, Class relativeTo)
  6. getRootAbsolutePathname(final Class clazz)
  7. getRunClassAllPath(Object obj)
  8. getSystemLibraryPath(Class locatorClass, String subDir)