Java ClassPath findClassPaths()

Here you can find the source of findClassPaths()

Description

Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH

License

BSD License

Declaration

@SuppressWarnings("deprecation")
public static URL[] findClassPaths() 

Method Source Code


//package com.java2s;
/* Chronos - Game Development Toolkit for Java game developers. The
 * original source remains://  w ww.ja v  a  2  s . c o m
 * 
 * Copyright (c) 2013 Miguel Gonzalez http://my-reality.de
 * 
 * This source is provided under the terms of the BSD License.
 * 
 * Copyright (c) 2013, Chronos
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the following 
 * conditions are met:
 * 
 *  * Redistributions of source code must retain the above 
 *    copyright notice, this list of conditions and the 
 *    following disclaimer.
 *  * Redistributions in binary form must reproduce the above 
 *    copyright notice, this list of conditions and the following 
 *    disclaimer in the documentation and/or other materials provided 
 *    with the distribution.
 *  * Neither the name of the Chronos/my Reality Development nor the names of 
 *    its contributors may be used to endorse or promote products 
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 */

import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import java.util.List;
import java.util.StringTokenizer;

public class Main {
    /**
     * Uses the java.class.path system property to obtain a list of URLs that
     * represent the CLASSPATH
     * 
     * @return
     */
    @SuppressWarnings("deprecation")
    public static URL[] findClassPaths() {
        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);
            try {
                list.add(fp.toURL());
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
        return list.toArray(new URL[list.size()]);
    }

    /**
     * Uses the java.class.path system property to obtain a list of URLs that
     * represent the CLASSPATH
     * <p/>
     * paths is used as a filter to only include paths that have the specific
     * relative file within it
     * 
     * @param paths
     *            comma list of files that should exist in a particular path
     * @return
     */
    @SuppressWarnings("deprecation")
    public static URL[] findClassPaths(String... paths) {
        ArrayList<URL> list = new ArrayList<URL>();

        String classpath = System.getProperty("java.class.path");
        StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
        for (int i = 0; i < paths.length; i++) {
            paths[i] = paths[i].trim();
        }

        while (tokenizer.hasMoreTokens()) {
            String path = tokenizer.nextToken().trim();
            boolean found = false;
            for (String wantedPath : paths) {
                if (path.endsWith(File.separator + wantedPath)) {
                    found = true;
                    break;
                }
            }
            if (!found)
                continue;
            File fp = new File(path);
            if (!fp.exists())
                throw new RuntimeException("File in java.class.path does not exists: " + fp);
            try {
                list.add(fp.toURL());
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
        return list.toArray(new URL[list.size()]);
    }
}

Related

  1. createClassPath(File file)
  2. dumpClasspath(ClassLoader loader)
  3. existsInClasspath(Class clazz, String fileName)
  4. existsInClasspath(final String fileName)
  5. fillClassPath(ClassLoader cl, StringBuffer classpath)
  6. findClassPaths()
  7. findClasspathsByLoader(ClassLoader loader)
  8. findClassPathsToEn()
  9. findDirectoryFromClasspath(Class cls, String file)