Java Utililty Methods Path to URL

List of utility methods to do Path to URL

Description

The list of methods to do Path to URL are organized into topic(s).

Method

URLtoUrl(final String path)
Convert a local file path to a URL
URL url = null;
if (path.startsWith(fileProtocolPrefix)) {
    url = new URL(path);
} else {
    try {
        url = new URL(path);
    } catch (Exception e) {
        File file = new File(path).getCanonicalFile();
...
URLtoURL(String path)
Returns an URL to the supplied file.
URL url;
try {
    url = new File(path).toURL();
} catch (MalformedURLException e) {
    url = ClassLoader.getSystemResource(path);
return url;
URLtoURL(String pathPrefix, String jar)
to URL
try {
    String path = jar.trim().replace(" ", "%20");
    if (!new File(path).isAbsolute())
        return new URL("file:" + pathPrefix + '/' + path);
    return new URL("file:" + path);
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
ListtoURLs(String... paths)
to UR Ls
List<URL> urls = new ArrayList<URL>(paths.length);
for (int i = 0; i < paths.length; ++i) {
    File file = new File(paths[i]);
    if (file.exists()) {
        urls.add(file.toURI().toURL());
return urls;
...
URL[]toURLs(String[] classPath)
to UR Ls
List<URL> urls = new ArrayList<URL>();
try {
    for (String x : classPath) {
        File file = new File(x);
        if (file.exists()) {
            urls.add(file.toURI().toURL());
    return urls.toArray(new URL[0]);
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
URLurl(final String path)
url
try {
    return new URL(path);
} catch (MalformedURLException e) {
    throw Throwables.propagate(e);
URLurl(String path)
url
if (path.startsWith("\\\\")) {
    path = path.replace("\\", "/");
return new URL("file:/" + (path.startsWith("/") ? "/" + path : path));
URLurl(String protocol, String host, int port, String path)
Creates URL using provided parameters.
if ((DEFAULT_HTTP_PORT == port && "http".equals(protocol))
        || (DEFAULT_HTTPS_PORT == port && "https".equals(protocol))) {
    port = -1;
try {
    return new URL(protocol, host, port, path);
} catch (MalformedURLException e) {
    throw new RuntimeException(e.getMessage(), e);
...