Java Utililty Methods URL Resolve

List of utility methods to do URL Resolve

Description

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

Method

Fileresolve(final URL url)
resolve
File resultFile = null;
URL resolved = url;
if (!url.getProtocol().equals("file")) { 
    throw new IllegalArgumentException("Unsupported protocol: " 
            + url.getProtocol());
try {
    resultFile = new File(resolved.toURI());
...
URLresolve(URL base, String relUrl)
Create a new absolute URL, from a provided existing absolute URL and a relative URL component.
if (relUrl.startsWith("?"))
    relUrl = base.getPath() + relUrl;
if (relUrl.indexOf('.') == 0 && base.getFile().indexOf('/') != 0) {
    base = new URL(base.getProtocol(), base.getHost(), base.getPort(), "/" + base.getFile());
return new URL(base, relUrl);
URLresolve(URL base, String uri)
Resolves specified uri to a specified base URL.
try {
    return new URL(base, uri);
} catch (MalformedURLException e) {
    if (WINDOWS_PATH.matcher(uri).matches()) {
        return new URL("file:/" + uri);
    } else { 
        throw e;
StringresolveGoogleRedirect(String url)
Exctract redirect target from google's redirect url
String query = new URI(url).getRawQuery();
for (String param : query.split("&"))
    if (param.contains("=")) {
        String[] parts = param.split("=");
        if (parts.length == 2 && parts[0].equals("url")) {
            return URLDecoder.decode(parts[1], "UTF-8");
throw new IllegalArgumentException("google redirect not found for url: " + url);
StringresolveHref(String baseUrl, String href)
This method takes an base url and resolves the href to an url
URL currentUrl = new URL(baseUrl);
if (href.startsWith("http://") || href.startsWith("https://")) {
    return href;
} else if (href.startsWith("//")) {
    return currentUrl.getProtocol() + ":" + href;
} else if (href.startsWith("?")) {
    return currentUrl.getProtocol() + "://" + currentUrl.getHost() + currentUrl.getPath() + href;
} else {
...
ListresolveManifestResources(final URL manifestUrl, final List resources)
resolve Manifest Resources
return resources.stream().map(name -> {
    try {
        return Optional.of(new URL(manifestUrl, "../" + name));
    } catch (final MalformedURLException e) {
        return Optional.<URL>empty();
}).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
StringresolvePlatformUrl(String urlspec)
resolve Platform Url
String result = null;
try {
    urlspec = urlspec.replace('\\', '/');
    URL url = new URL(urlspec);
    URL resolvedURL = FileLocator.resolve(url);
    result = resolvedURL.toString();
} catch (Exception e) {
return result;
URLresolveRelativeURL(final URL primaryUrl, final String relativeLoc)
Create a new URL by resolving a given relative string (such as "mydir/myfile.ext") against a given url (such as "http://www.company.com/mycontent/content.html").
String url = primaryUrl.toString();
url = url.replaceAll("\\%2[F,f]", "/");
return new URL(new URL(url), relativeLoc);
StringresolveRelativeURL(String contextUri, String relativeUri)
resolve Relative URL
try {
    URL contextUrl = new URL(contextUri);
    URL absoluteUrl = new URL(contextUrl, relativeUri);
    String result = absoluteUrl.toString();
    return result;
} catch (MalformedURLException e) {
    throw new IllegalArgumentException(e);
URLresolveResourceAsURL(Class clazz, String name)
Same as java.lang.Class#getResource() but works with and without jars reliably.
URL url = clazz.getResource(name);
if (url == null) {
    name = name.startsWith("/") ? name.substring(1) : "/" + name;
    url = clazz.getResource(name);
return url;