Java ClassPath fillClassPath(ClassLoader cl, StringBuffer classpath)

Here you can find the source of fillClassPath(ClassLoader cl, StringBuffer classpath)

Description

Walk the classloader hierarchy and add to the classpath

License

Apache License

Parameter

Parameter Description
cl a parameter
classpath a parameter

Declaration

private static void fillClassPath(ClassLoader cl, StringBuffer classpath) 

Method Source Code

//package com.java2s;
/*//from  w ww . j  a va  2 s. co  m
 * ClasspathUtils.java
 * 
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.StringTokenizer;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

public class Main {
    /**
     * Walk the classloader hierarchy and add to the classpath
     * @param cl
     * @param classpath
     */
    private static void fillClassPath(ClassLoader cl, StringBuffer 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 it is a drive letter, adjust accordingly.
                    if (path.length() >= 3 && path.charAt(0) == '/'
                            && path.charAt(2) == ':')
                        path = path.substring(1);
                    classpath.append(URLDecoder.decode(path));
                    classpath.append(File.pathSeparatorChar);

                    // if its a jar extract Class-Path entries from manifest
                    File file = new File(urls[i].getFile());
                    if (file.isFile()) {
                        FileInputStream fis = null;
                        try {
                            fis = new FileInputStream(file);
                            if (isJar(fis)) {
                                JarFile jar = new JarFile(file);
                                Manifest manifest = jar.getManifest();
                                if (manifest != null) {
                                    Attributes attributes = manifest
                                            .getMainAttributes();
                                    if (attributes != null) {
                                        String s = attributes
                                                .getValue(Attributes.Name.CLASS_PATH);
                                        String base = file.getParent();
                                        if (s != null) {
                                            StringTokenizer st = new StringTokenizer(
                                                    s, " ");
                                            while (st.hasMoreTokens()) {
                                                String t = st.nextToken();
                                                classpath
                                                        .append(base
                                                                + File.separatorChar
                                                                + t);
                                                classpath
                                                        .append(File.pathSeparatorChar);
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (IOException ioe) {
                        } finally {
                            if (fis != null) {
                                try {
                                    fis.close();
                                } catch (IOException ioe2) {
                                }
                            }
                        }
                    }
                }
            }
            cl = cl.getParent();
        }
    }

    /**
     * Check if this inputstream is a jar/zip
     * @param is
     * @return true if inputstream is a jar
     */
    public static boolean isJar(InputStream is) {
        try {
            JarInputStream jis = new JarInputStream(is);
            if (jis.getNextEntry() != null) {
                return true;
            }
        } catch (IOException ioe) {
        }
        return false;
    }
}

Related

  1. createClassLoader(List libs, boolean useSystemClassPath)
  2. createClassPath(File file)
  3. dumpClasspath(ClassLoader loader)
  4. existsInClasspath(Class clazz, String fileName)
  5. existsInClasspath(final String fileName)
  6. findClassPaths()
  7. findClassPaths()
  8. findClasspathsByLoader(ClassLoader loader)
  9. findClassPathsToEn()