Java Utililty Methods ClassPath

List of utility methods to do ClassPath

Description

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

Method

StringcalculateClassPath(ClassLoader cl)
calculate local file based class path for class loader if possible (servlet classes must be located there)
StringBuffer classPath = new StringBuffer();
boolean jspFound = false, servletFound = false;
while (cl != null) {
    if (cl instanceof URLClassLoader) {
        boolean addClasses = false;
        if (jspFound == false) {
            jspFound = ((URLClassLoader) cl).findResource("javax/servlet/jsp/JspPage.class") != null;
            addClasses |= jspFound;
...
URLClassLoadercreateClassLoader(List classpathElements, String... paths)
create Class Loader
List<URL> urls = new ArrayList<>();
for (String path : paths) {
    URL url = pathToUrl(path);
    urls.add(url);
for (Object object : classpathElements) {
    if (object != null) {
        String path = object.toString();
...
ClassLoadercreateClassLoader(List libs, boolean useSystemClassPath)
create Class Loader
List<URL> urls = new ArrayList<>(libs.size());
libs.forEach(lib -> {
    try {
        urls.add((new File(lib)).toURI().toURL());
    } catch (MalformedURLException e) {
        throw new RuntimeException("The file wasn't readable: " + lib, e);
});
...
URL[]createClassPath(File file)
create Class Path
URL[] urls = null;
if ((file.exists()) && (file.isDirectory())) {
    String[] jars = file.list(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            if (name.endsWith(".jar")) {
                return true;
            return false;
...
voiddumpClasspath(ClassLoader loader)
dump Classpath
System.out.println("Classloader " + loader + ":");
if (loader instanceof URLClassLoader) {
    URLClassLoader ucl = (URLClassLoader) loader;
    System.out.println("\t" + Arrays.toString(ucl.getURLs()));
} else
    System.out.println("\t(cannot display components as not a URLClassLoader)");
if (loader.getParent() != null)
    dumpClasspath(loader.getParent());
...
booleanexistsInClasspath(Class clazz, String fileName)
exists In Classpath
URL dirURL = clazz.getResource("/" + fileName);
return dirURL != null;
booleanexistsInClasspath(final String fileName)
exists In Classpath
return getResourceFromClasspath(fileName) != null;
voidfillClassPath(ClassLoader cl, StringBuffer classpath)
Walk the classloader hierarchy and add to the classpath
while (cl != null) {
    if (cl instanceof URLClassLoader) {
        URL[] urls = ((URLClassLoader) cl).getURLs();
        for (int i = 0; (urls != null) && i < urls.length; i++) {
            String path = urls[i].getPath();
            if (path.length() >= 3 && path.charAt(0) == '/' && path.charAt(2) == ':')
                path = path.substring(1);
            classpath.append(URLDecoder.decode(path));
...
ListfindClassPaths()
find Class Paths
List<URL> list = new ArrayList<URL>();
String classpath = System.getProperty("java.class.path");
StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
    String path = tokenizer.nextToken();
    File fp = new File(path);
    if (!fp.exists())
        throw new RuntimeException("File in java.class.path does not exist: " + fp);
...
URL[]findClassPaths()
Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH
List<URL> list = new ArrayList<URL>();
String classpath = System.getProperty("java.class.path");
StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
    String path = tokenizer.nextToken();
    File fp = new File(path);
    if (!fp.exists())
        throw new RuntimeException("File in java.class.path does not exist: " + fp);
...