Java Path to URL getURLFromPath(String jarPath)

Here you can find the source of getURLFromPath(String jarPath)

Description

get the URL from a given path string

License

LGPL

Parameter

Parameter Description
jarPath The path that represents a URL

Exception

Parameter Description
IllegalArgumentException is something goes wrong

Return

The resolved URL reference

Declaration

public static URL getURLFromPath(String jarPath) 

Method Source Code

//package com.java2s;
/*/* www.  j a v a2  s . c  o m*/
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * get the URL from a given path string
     *
     * @param jarPath The path that represents a URL
     *
     * @return The resolved URL reference
     *
     * @throws IllegalArgumentException is something goes wrong
     */
    public static URL getURLFromPath(String jarPath) {
        URL jarUrl;
        try {
            //is it an url
            jarUrl = new URL(jarPath);
        } catch (MalformedURLException e) {
            try {
                //consider it as a file path
                jarUrl = new URL("file:" + jarPath);
            } catch (MalformedURLException ee) {
                throw new IllegalArgumentException("Unable to find jar:" + jarPath, ee);
            }
        }
        return jarUrl;
    }
}

Related

  1. getUrl(String pluginId, String relativePath)
  2. getURL(String schema, String host, String path)
  3. getUrl(URL base, String path)
  4. getUrlForLocalPath(String path)
  5. getURLFromClassPath(String source)
  6. getUrlFromPath(String path)
  7. getUrlFromUrlPath(String path)
  8. getURLPath(URI uri)
  9. getURLPath(URL repositoryURL)