This function finds the first matching filename for a Java class file from the classpath, if none is found it returns null. - Java Reflection

Java examples for Reflection:Class Loader

Description

This function finds the first matching filename for a Java class file from the classpath, if none is found it returns null.

Demo Code

/**//from  www  .  ja v a2 s .  c o m
 * Distribution License:
 * JSword is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License, version 2.1 as published by
 * the Free Software Foundation. This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The License is available on the internet at:
 *       http://www.gnu.org/copyleft/lgpl.html
 * or by writing to:
 *      Free Software Foundation, Inc.
 *      59 Temple Place - Suite 330
 *      Boston, MA 02111-1307, USA
 *
 * Copyright: 2005
 *     The copyright to this program is held by it's authors.
 *
 * ID: $Id: ClassUtil.java 2230 2012-02-08 00:00:10Z dmsmith $
 */
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main{
    public static void main(String[] argv) throws Exception{
        String classname = "java2s.com";
        System.out.println(findClasspathEntry(classname));
    }
    private static final String EXTENSION_CLASS = ".class";
    private static final String EXTENSION_JAR = ".jar";
    private static final String EXTENSION_ZIP = ".zip";
    /**
     * The log stream
     */
    private static final Logger log = Logger.getLogger(ClassUtil.class);
    /**
     * This function finds the first matching filename for a Java class file
     * from the classpath, if none is found it returns null.
     */
    public static String findClasspathEntry(String classname,
            String classpath) {
        String full = null;

        String[] paths = StringUtil.split(classpath, File.pathSeparator);
        for (int i = 0; i < paths.length; i++) {
            // Search the jar
            if (paths[i].endsWith(EXTENSION_ZIP)
                    || paths[i].endsWith(EXTENSION_JAR)) {
                ZipFile zip = null;
                try {
                    String fileName = classname.replace(',', '/')
                            + EXTENSION_CLASS;
                    zip = new ZipFile(paths[i]);
                    ZipEntry entry = zip.getEntry(fileName);

                    if (entry != null && !entry.isDirectory()) {
                        if (full != null && !full.equals(fileName)) {
                            log.warn("Warning duplicate " + classname
                                    + " found: " + full + " and "
                                    + paths[i]);
                        } else {
                            full = paths[i];
                        }
                    }
                } catch (IOException ex) {
                    // If that zip file failed, then ignore it and move on.
                    log.warn("Missing zip file for " + classname + " and "
                            + paths[i]);
                } finally {
                    IOUtil.close(zip);
                }
            } else {
                StringBuilder path = new StringBuilder(256);

                // Search for the file
                String extra = classname.replace('.', File.separatorChar);

                path.append(paths[i]);
                if (paths[i].charAt(paths[i].length() - 1) != File.separatorChar) {
                    path.append(File.separatorChar);
                }

                path.append(extra);
                path.append(EXTENSION_CLASS);
                String fileName = path.toString();

                if (new File(fileName).isFile()) {
                    if (full != null && !full.equals(fileName)) {
                        log.warn("Warning duplicate " + classname
                                + " found: " + full + " and " + paths[i]);
                    } else {
                        full = paths[i];
                    }
                }
            }
        }

        return full;
    }
    /**
     * This function find the first matching filename for a Java class file from
     * the classpath, if none is found it returns null.
     */
    public static String findClasspathEntry(String classname) {
        String classpath = System.getProperty("java.class.path", "");
        return findClasspathEntry(classname, classpath);
    }
}

Related Tutorials