Java Class Path Create getClasspathURLs(String classpath)

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

Description

Utility method that converts the components of a String representing a classpath into file URL(s).

License

Open Source License

Parameter

Parameter Description
classpath <code>String</code> containing components separated by path separators that represent the components making up a classpath

Exception

Parameter Description

Return

a URL[] where each element of the array corresponds to one of the components in the classpath parameter. The path components are (potentially) expanded via File.getCanonicalFile() before converting to a URL format.

Declaration

public static URL[] getClasspathURLs(String classpath) throws IOException, MalformedURLException 

Method Source Code


//package com.java2s;
/*/*from  w ww.  ja  v  a2s.com*/
 * 
 * Copyright 2005 Sun Microsystems, Inc.
 * 
 * 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.MalformedURLException;
import java.net.URL;

import java.util.StringTokenizer;

public class Main {
    /**
     * Utility method that converts the components of a <code>String</code> representing a classpath
     * into file <code>URL</code>(s).
     *
     * @param classpath <code>String</code> containing components separated by path separators that
     *                  represent the components making up a classpath
     * @return a <code>URL[]</code> where each element of the array corresponds to one of the
     * components in the <code>classpath</code> parameter. The path components are (potentially)
     * expanded via <code>File.getCanonicalFile()</code> before converting to a <code>URL</code>
     * format.
     * @throws java.net.MalformedURLException If the path cannot be parsed as a URL
     * @throws java.net.IOException           If an I/O error occurs, which is possible because the
     *                                        construction of the canonical pathname may require
     *                                        filesystem queries
     */
    public static URL[] getClasspathURLs(String classpath) throws IOException, MalformedURLException {
        StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
        URL[] urls = new URL[st.countTokens()];
        for (int i = 0; st.hasMoreTokens(); i++) {
            urls[i] = new File(st.nextToken()).getCanonicalFile().toURI().toURL();
        }
        return urls;
    }
}

Related

  1. getClassBasePath(URL classUrl, Class clazz)
  2. getClassPathArchiveContents(final URL resource)
  3. getClasspathResourceURL(String resourceName)
  4. getClasspathUrl(final Class aClass, final String aPath)
  5. getClasspathURL(final String name)