Java URI.resolve

I'm trying to resolve two URIs, but it's not as straightforward as I'd like it to be.

URI a = new URI("");
URI b = new URI("bar.html");

The trouble is that a.resolve(b).toString() is now "". How can I get away with that?

Sounds like you probably want to use URL rather than URI (which is more general and needs to deal with a less strict syntax.)

URI a = new URI("");
URI b = new URI("bar.html");
URI c = a.resolve(b);
c.toString()     -> ""
c.getAuthority() -> ""
c.getPath()      -> "bar.html"

URI's toString() doesn't behave as you might expect, but given its general nature it may be that it should be forgiven.

Sadly URI's toURL() method doesn't behave quite as I would have hoped to give you what you want.

URL u = c.toURL();
u.toString()     -> ""
u.getAuthority() -> ""  --- Oh dear :(

So best just to start straight out with a URL to get what you want:

URL x = new URL("");
URL y = new URL(x, "bar.html");
y.toString() -> ""

URI (Java Platform SE 7 ), resolve(u.relativize(v)).equals(v) . This operation is often useful when constructing a document containing URIs that must be made relative to the� Java URI.resolve. Ask Question Asked 10 years, 3 months ago. Active 7 months ago. Viewed 18k times 19. 4. I'm trying to resolve two URIs, but it's not as

URI should contain the final separator('/') as well to resolve the way you want:

URI a = new URI(""); java code examples, @param relative * the given string to create the new URI instance which has to * be resolved later on. * @return the created and resolved URI. */ public URI� Java . This code will resolve the relative base uri e.g. if current base uri is /path1/path2 then ../path3 will be resolved as /path1/path3.

Ok, appears from URL deffinition scheme://domain:port/path?query_string#fragment_id there should be 3 slashes before path (two after scheme and one directly before path) 2 situation can occure:

    there are 3 slashes in your URI => everything is OK
    there are less then 3 slashes in your URI => you need to add slash at the end of URI

there is my snappet of code:

String url = "";
String endSlash="";
int indexOfSlash = 0;
for(int i = 0;i<3;i++){
   int nextIndex = url.indexOf('/',indexOfSlash);
         throw new MalformedURLException("Bad given url format, mising :// after schema");
      indexOfSlash = ++nextIndex;
URL rightUrl = new URL(url+endSlash);

Java Code Examples for, In the following code shows how to use URI.resolve(String str) method. import; import; public class Main { public static� The following are Jave code examples for showing how to use resolve() of the class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.

URI.resolve behaves like if you are on a HTML page like and click a link with href="page1.html": It cuts off the last segment (here menu.html) and puts page1.html in its place.

(, page1.html) →

This works also, if the object you call resolve on is a directory, denoted by ending in a slash:

(, page1.html) →

If it does not end in a slash, the outcome is not what you might expect:

(, page1.html) → (missing "to")

If you know that the first argument of the URIs to concatenate is a directory, but you don’t know in which format you get it (with or without trailing slash), this might help you:

static URI asDirectory(URI uri) {
    String uriString = uri.toString();
    return !uriString.endsWith("/") ? URI.create(uriString.concat("/")) : uri;

Java API Tutorial - Java URI.resolve(String str), Syntax : public URI resolve(URI uri) Parameters : uri : URI to be resolved. Another overloaded method which takes string as argument and is equivalent to calling� This convenience method works as if invoking it were equivalent to evaluating the expression {@link #resolve( resolve}(URI.{@link #create(String) create}(str)). Parameters: str - The string to be parsed into a URI class in Java, Java. This code will resolve the relative base uri e.g. if current base uri is /path1/ path2 then ../path3 will be resolved as /path1/path3. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

How to resolve relative URI in Java?, Creates a new URI instance by parsing the given string relative and resolves the created URI against the URI represented by this instance. Resolve(URI) Resolve( � I can observe the issue on jdk1.7.0_71, jdk1.8.0_211, but 9, 10, 11, 12, 13 and mainline behave properly.

URI.Resolve Method (Java.Net), URI. However, the standard Java class has a bug. The Java URI class does It depends on the protocol (scheme) used which root is used to resolve the path. FULL PRODUCT VERSION : java version "1.5.0" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64) Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode) ADDITIONAL OS VERSION INFORMATION : Microsoft Windows XP [Version 5.1.2600] A DESCRIPTION OF THE PROBLEM : An exception is thrown where one should not be.

  • What was the output you were expecting?
  • "" would have been fine.
  • Why are you using two URIs? There must be a reason but it's unclear.