Java ClassPath parseClassPath(String classpath)

Here you can find the source of parseClassPath(String classpath)

Description

Parse a Java classpath string to URLs.

License

Apache License

Parameter

Parameter Description
classpath the classpath string.

Exception

Parameter Description
IOException on IO errors.

Return

URLs.

Declaration

public static URL[] parseClassPath(String classpath) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Expedia Inc.//  w  w  w  .  jav a2 s  .  c o m
 *
 * 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.IOException;
import java.net.URL;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Parse a Java {@code classpath} string to URLs. The {@code classpath} is represented with the same format of the 
     * {@code CLASSPATH} variable or {@code -cp} java option. It supports the {@code *} wildcard.
     * 
     * @param classpath
     *       the classpath string.
     * @return
     *       URLs.
     * 
     * @throws IOException
     *       on IO errors.
     */
    public static URL[] parseClassPath(String classpath) throws IOException {
        final String splitPattern = Character.toString(File.pathSeparatorChar);
        final String wildcardPattern = ".+\\*";
        String[] paths = classpath.split(splitPattern);
        List<URL> urls = new ArrayList<URL>();
        for (String path : paths) {
            path = path.trim();
            if (path.matches(wildcardPattern)) {
                File folder = new File(path.replace("\\*", ""));
                if (folder.exists()) {
                    File[] files = folder.listFiles();
                    for (File f : files) {
                        urls.add(f.toURI().toURL());
                    }
                }
            } else {
                urls.add(new File(path).toURI().toURL());
            }
        }
        URL[] result = new URL[urls.size()];
        return urls.toArray(result);
    }
}

Related

  1. isInClassPath(String location)
  2. loadDirectoryFromClasspath(String path)
  3. loadFromClasspath(Class clazz, String fileName)
  4. loadFromClasspath(final String configFileName)
  5. loadResourceFromClasspath(Class clazz, String resourceName)
  6. parseJavaClassPath()
  7. printClassPath()
  8. printClassPath(boolean single)
  9. printJVMClassPath()