Java : URI with Examples

URI (Java SE 21 & JDK 21) with Examples.
You will find code examples on most URI methods.


Summary

Represents a Uniform Resource Identifier (URI) reference.

Class diagram

final var uri = new URI("https://example.com/java/index.html");
System.out.println(uri); // "https://example.com/java/index.html"
System.out.println(uri.getScheme()); // "https"
System.out.println(uri.getHost()); // "example.com"
System.out.println(uri.getPath()); // "/java/index.html"

Constructors

URI (String str)

Constructs a URI by parsing the given string.

final var ret = new URI("");
System.out.println(ret); // ""
// Hierarchical URIs. (absolute URIs)
final var ret1 = new URI("http://example.com/languages/java/");
System.out.println(ret1); // "http://example.com/languages/java/"

final var ret2 = new URI("file:///~/calendar");
System.out.println(ret2); // "file:///~/calendar"
// Hierarchical URIs. (relative URIs)
final var ret1 = new URI("sample/a/index.html#28");
System.out.println(ret1); // "sample/a/index.html#28"

final var ret2 = new URI("../../demo/b/index.html");
System.out.println(ret2); // "../../demo/b/index.html"
// Opaque URIs.
final var ret1 = new URI("mailto:java-net@www.example.com");
System.out.println(ret1); // "mailto:java-net@www.example.com"

final var ret2 = new URI("news:comp.lang.java");
System.out.println(ret2); // "news:comp.lang.java"

final var ret3 = new URI("urn:isbn:096139210x");
System.out.println(ret3); // "urn:isbn:096139210x"
// Escape.
final var uri1 = new URI("http://example.com/♪");
System.out.println(uri1); // "http://example.com/♪"
System.out.println(uri1.toASCIIString()); // "http://example.com/%E2%99%AA"
System.out.println(uri1.getRawPath()); // "/♪"
System.out.println(uri1.getPath()); // "/♪"

final var uri2 = new URI("http://example.com/%E2%99%AA");
System.out.println(uri2); // "http://example.com/%E2%99%AA"
System.out.println(uri2.toASCIIString()); // "http://example.com/%E2%99%AA
System.out.println(uri2.getRawPath()); // "/%E2%99%AA"
System.out.println(uri2.getPath()); // "/♪"
try {
    final var uri = new URI("a b");
} catch (URISyntaxException e) {
    System.out.println("URISyntaxException! : " + e.getMessage());
}

// Result
// ↓
//URISyntaxException! : Illegal character in path at index 1: a b

URI (String scheme, String ssp, String fragment)

Constructs a URI from the given components.

final var ret1 = new URI(null, null, null);
System.out.println(ret1); // ""

final var ret2 = new URI(null, null, "fragment");
System.out.println(ret2); // "#fragment"
// Hierarchical URIs. (absolute URIs)
final var ret1 = new URI("http", "//example.com/languages/java/index.html", "fragment");
System.out.println(ret1); // "http://example.com/languages/java/index.html#fragment"

final var ret2 = new URI("http", "//example.com/languages/java/", null);
System.out.println(ret2); // "http://example.com/languages/java/"

final var ret3 = new URI("file", "///~/calendar", null);
System.out.println(ret3); // "file:///~/calendar"
// Hierarchical URIs. (relative URIs)
final var ret1 = new URI(null, "sample/a/index.html", "28");
System.out.println(ret1); // "sample/a/index.html#28"

final var ret2 = new URI(null, "../../demo/b/index.html", null);
System.out.println(ret2); // "../../demo/b/index.html"
// Opaque URIs.
final var ret1 = new URI("mailto", "java-net@www.example.com", null);
System.out.println(ret1); // "mailto:java-net@www.example.com"

final var ret2 = new URI("news", "comp.lang.java", null);
System.out.println(ret2); // "news:comp.lang.java"

final var ret3 = new URI("urn", "isbn:096139210x", null);
System.out.println(ret3); // "urn:isbn:096139210x"
// Escape.
final var uri1 = new URI("http", "//example.com/index.html", "♪");
System.out.println(uri1); // "http://example.com/index.html#♪"
System.out.println(uri1.toASCIIString()); // "http://example.com/index.html#%E2%99%AA"
System.out.println(uri1.getRawFragment()); // "♪"
System.out.println(uri1.getFragment()); // "♪"

// % → %25
final var uri2 = new URI("http", "//example.com/index.html", "%");
System.out.println(uri2); // "http://example.com/index.html#%25"
System.out.println(uri2.toASCIIString()); // "http://example.com/index.html#%25"
System.out.println(uri2.getRawFragment()); // "%25"
System.out.println(uri2.getFragment()); // "%"

// space → %20
final var uri = new URI("http", "//example.com/index.html", "a b");
System.out.println(uri); // "http://example.com/index.html#a%20b"
System.out.println(uri.toASCIIString()); // "http://example.com/index.html#a%20b"
System.out.println(uri.getRawFragment()); // "a%20b"
System.out.println(uri.getFragment()); // "a b"
try {
    final var uri = new URI("http", null, null);
} catch (URISyntaxException e) {
    System.out.println("URISyntaxException! : " + e.getMessage());
}

// Result
// ↓
//URISyntaxException! : Expected scheme-specific part at index 5: http:

URI (String scheme, String userInfo, String host, int port, String path, String query, String fragment)

Constructs a hierarchical URI from the given components.

final var ret1 = new URI(null, null, null, -1, null, null, null);
System.out.println(ret1); // ""

final var ret2 = new URI("http", "user", "example.com", -1, null, null, null);
System.out.println(ret2); // "http://user@example.com"

final var ret3 = new URI("http", null, "example.com", 80, null, null, null);
System.out.println(ret3); // "http://example.com:80"

final var ret4 = new URI("http", "user", "example.com", 80, null, null, null);
System.out.println(ret4); // "http://user@example.com:80"

final var ret5 = new URI("http", null, "example.com", -1, "/sample", "query", "fragment");
System.out.println(ret5); // "http://example.com/sample?query#fragment"
// Hierarchical URIs. (absolute URIs)
final var ret1 = new URI("http", null, "example.com", -1, "/languages/java/", null, null);
System.out.println(ret1); // "http://example.com/languages/java/"

final var ret2 = new URI("file", null, null, -1, "///~/calendar", null, null);
System.out.println(ret2); // "file:///~/calendar"
// Hierarchical URIs. (relative URIs)
final var ret1 = new URI(null, null, null, -1, "sample/a/index.html", null, "28");
System.out.println(ret1); // "sample/a/index.html#28"

final var ret2 = new URI(null, null, null, -1, "sample/a/index.html", "query", "fragment");
System.out.println(ret2); // "sample/a/index.html?query#fragment"

final var ret3 = new URI(null, null, null, -1, "../../demo/b/index.html", null, null);
System.out.println(ret3); // "../../demo/b/index.html"
// Escape.
final var uri1 = new URI("http", "×", "example.com", -1, "/♪", "〇", "△");
System.out.println(uri1); // "http://×@example.com/♪?〇#△"
System.out.println(uri1.toASCIIString()); // "http://%C3%97@example.com/%E2%99%AA?%E3%80%87#%E2%96%B3"
System.out.println(uri1.getRawUserInfo()); // "×"
System.out.println(uri1.getUserInfo()); // "×"
System.out.println(uri1.getRawPath()); // "/♪"
System.out.println(uri1.getPath()); // "/♪"
System.out.println(uri1.getRawQuery()); // "〇"
System.out.println(uri1.getQuery()); // "〇"
System.out.println(uri1.getRawFragment()); // "△"
System.out.println(uri1.getFragment()); // "△"

// % → %25
final var uri2 = new URI("http", "%a", "example.com", -1, "/%b", "%c", "%d");
System.out.println(uri2); // "http://%25a@example.com/%25b?%25c#%25d"
System.out.println(uri2.toASCIIString()); // "http://%25a@example.com/%25b?%25c#%25d"
System.out.println(uri2.getRawUserInfo()); // "%25a"
System.out.println(uri2.getUserInfo()); // "%a"
System.out.println(uri2.getRawPath()); // "/%25b"
System.out.println(uri2.getPath()); // "/%b"
System.out.println(uri2.getRawQuery()); // "%25c"
System.out.println(uri2.getQuery()); // "%c"
System.out.println(uri2.getRawFragment()); // "%25d"
System.out.println(uri2.getFragment()); // "%d"

// space → %20
final var uri3 = new URI("http", "u ser", "example.com", -1, "/pa th", "que ry", "frag ment");
System.out.println(uri3); // "http://u%20ser@example.com/pa%20th?que%20ry#frag%20ment"
System.out.println(uri3.toASCIIString()); // "http://u%20ser@example.com/pa%20th?que%20ry#frag%20ment"
System.out.println(uri3.getRawUserInfo()); // "u%20ser"
System.out.println(uri3.getUserInfo()); // "u ser"
System.out.println(uri3.getRawPath()); // "/pa%20th"
System.out.println(uri3.getPath()); // "/pa th"
System.out.println(uri3.getRawQuery()); // "que%20ry"
System.out.println(uri3.getQuery()); // "que ry"
System.out.println(uri3.getRawFragment()); // "frag%20ment"
System.out.println(uri3.getFragment()); // "frag ment"
try {
    final var uri = new URI("http", null, null, -1, null, null, null);
} catch (URISyntaxException e) {
    System.out.println("URISyntaxException! : " + e.getMessage());
}

// Result
// ↓
//URISyntaxException! : Expected scheme-specific part at index 5: http:

URI (String scheme, String host, String path, String fragment)

Constructs a hierarchical URI from the given components.

This constructor works as if by invoking the seven-argument constructor as follows:

new URI(scheme, null, host, -1, path, null, fragment)

URI (String scheme, String authority, String path, String query, String fragment)

Constructs a hierarchical URI from the given components.

final var ret1 = new URI(null, null, null, null, null);
System.out.println(ret1); // ""

final var ret2 = new URI("http", "example.com", null, null, null);
System.out.println(ret2); // "http://example.com"

final var ret3 = new URI("http", "user@example.com:80", null, null, null);
System.out.println(ret3); // "http://user@example.com:80"

final var ret4 = new URI("http", "user@example.com:80", "/sample", "query", "fragment");
System.out.println(ret4); // "http://user@example.com:80/sample?query#fragment"
// Hierarchical URIs. (absolute URIs)
final var ret1 = new URI("http", "example.com", "/languages/java/", null, null);
System.out.println(ret1); // "http://example.com/languages/java/"

final var ret2 = new URI("file", null, "///~/calendar", null, null);
System.out.println(ret2); // "file:///~/calendar"
// Hierarchical URIs. (relative URIs)
final var ret1 = new URI(null, null, "sample/a/index.html", null, "28");
System.out.println(ret1); // "sample/a/index.html#28"

final var ret2 = new URI(null, null, "../../demo/b/index.html", null, null);
System.out.println(ret2); // "../../demo/b/index.html"
// Escape.
final var uri1 = new URI("http", "example.com", "/♪", "〇", "△");
System.out.println(uri1); // "http://example.com/♪?〇#△"
System.out.println(uri1.toASCIIString()); // "http://example.com/%E2%99%AA?%E3%80%87#%E2%96%B3"
System.out.println(uri1.getRawPath()); // "/♪"
System.out.println(uri1.getPath()); // "/♪"
System.out.println(uri1.getRawQuery()); // "〇"
System.out.println(uri1.getQuery()); // "〇"
System.out.println(uri1.getRawFragment()); // "△"
System.out.println(uri1.getFragment()); // "△"

// % → %25
final var uri2 = new URI("http", "example.com", "/%a", "%b", "%c");
System.out.println(uri2); // "http://example.com/%25a?%25b#%25c"
System.out.println(uri2.toASCIIString()); // "http://example.com/%25a?%25b#%25c"
System.out.println(uri2.getRawPath()); // "/%25a"
System.out.println(uri2.getPath()); // "/%a"
System.out.println(uri2.getRawQuery()); // "%25b"
System.out.println(uri2.getQuery()); // "%b"
System.out.println(uri2.getRawFragment()); // "%25c"
System.out.println(uri2.getFragment()); // "%c"

// space → %20
final var uri3 = new URI("http", "example.com", "/pa th", "que ry", "frag ment");
System.out.println(uri3); // "http://example.com/pa%20th?que%20ry#frag%20ment"
System.out.println(uri3.toASCIIString()); // "http://example.com/pa%20th?que%20ry#frag%20ment"
System.out.println(uri3.getRawPath()); // "/pa%20th"
System.out.println(uri3.getPath()); // "/pa th"
System.out.println(uri3.getRawQuery()); // "que%20ry"
System.out.println(uri3.getQuery()); // "que ry"
System.out.println(uri3.getRawFragment()); // "frag%20ment"
System.out.println(uri3.getFragment()); // "frag ment"
try {
    final var uri = new URI("http", null, null, null, null);
} catch (URISyntaxException e) {
    System.out.println("URISyntaxException! : " + e.getMessage());
}

// Result
// ↓
//URISyntaxException! : Expected scheme-specific part at index 5: http:

Methods

int compareTo (URI that)

Compares this URI to another object, which must be a URI.

final var uri1 = URI.create("http://example.com/a");
final var uri2 = URI.create("http://example.com/a");
final var uri3 = URI.create("http://example.com/A");

System.out.println(uri1.compareTo(uri2)); // 0 ("a" = "a")
System.out.println(uri1.compareTo(uri3)); // 32 ("a" > "A")

final var uri4 = URI.create("http://example.com/b");
final var uri5 = URI.create("http://example.com/B");

System.out.println(uri1.compareTo(uri4)); // -1 ("a" < "b")
System.out.println(uri1.compareTo(uri5)); // 31 ("a" > "B")
final var uri1 = URI.create("http://example.com/a");
final var uri2 = URI.create("http://example.com/a?b");
final var uri3 = URI.create("http://example.com/a?c");
final var uri4 = URI.create("http://example.com/a?B");

System.out.println(uri1.compareTo(uri2)); // -1 ("a" < "a?b")
System.out.println(uri2.compareTo(uri3)); // -1 ("a?b" < "a?c")
System.out.println(uri2.compareTo(uri4)); // 32 ("a?b" > "a?B")

final var uri5 = URI.create("http://example.com/a#b");
final var uri6 = URI.create("http://example.com/a#c");
final var uri7 = URI.create("http://example.com/a#B");

System.out.println(uri1.compareTo(uri5)); // -1 ("a" < "a#b")
System.out.println(uri2.compareTo(uri5)); // 1  ("a?b" > "a#b")
System.out.println(uri5.compareTo(uri6)); // -1 ("a#b" < "a#c")
System.out.println(uri5.compareTo(uri7)); // 32 ("a#b" > "a#B")

final var uri8 = URI.create("http://example.com/a?b#b");
final var uri9 = URI.create("http://example.com/a?b#c");
final var uri10 = URI.create("http://example.com/a?b#B");

System.out.println(uri1.compareTo(uri8)); // -1 ("a" < "a?b#b")
System.out.println(uri2.compareTo(uri8)); // -1 ("a?b" < "a?b#b")
System.out.println(uri5.compareTo(uri8)); // -1 ("a#b" < "a?b#b")
System.out.println(uri8.compareTo(uri9)); // -1 ("a?b#b" < "a?b#c")
System.out.println(uri8.compareTo(uri10)); // 32 ("a?b#b" > "a?b#B")
// Two URIs with schemes are compared case-insensitively.
final var uri1 = URI.create("http://example.com/a");

final var uri2 = URI.create("https://example.com/a");
final var uri3 = URI.create("HTTP://example.com/a");
final var uri4 = URI.create("HTTPS://example.com/a");
final var uri5 = URI.create("aaaa://example.com/a");

System.out.println(uri1.compareTo(uri2)); // -1 ("http" < "https")
System.out.println(uri1.compareTo(uri3)); // 0 ("http" = "HTTP")
System.out.println(uri1.compareTo(uri4)); // -1 ("http" < "HTTPS")
System.out.println(uri1.compareTo(uri5)); // 7 ("http" > "aaaa")
// A hierarchical URI is considered to be less than
// an opaque URI with an identical scheme.
final var uri1 = URI.create("http://www.example.com");

final var uri2 = URI.create("http:example.com");
final var uri3 = URI.create("http:www.example.com");

System.out.println(uri1.compareTo(uri2)); // -1
System.out.println(uri1.compareTo(uri3)); // -1
// Opaque URIs with an identical scheme.
final var uri1 = URI.create("mailto:aaa@example.com");
final var uri2 = URI.create("mailto:bbb@example.com");
final var uri3 = URI.create("mailto:BBB@example.com");

System.out.println(uri1.compareTo(uri2)); // -1 ("aaa" < "bbb")
System.out.println(uri1.compareTo(uri3)); // 31 ("aaa" > "BBB")

final var uri4 = URI.create("mailto:aaa@example.com#xxx");
final var uri5 = URI.create("mailto:aaa@example.com#yyy");

System.out.println(uri1.compareTo(uri4)); // -1 (null < "xxx")
System.out.println(uri1.compareTo(uri5)); // -1 (null < "yyy")
System.out.println(uri5.compareTo(uri4)); // 1 ("yyy" > "xxx")
// Two URIs with hosts are compared case-insensitively.
final var uri1 = URI.create("http://example.com/a");
final var uri2 = URI.create("http://www.example.com/a");
final var uri3 = URI.create("http://EXAMPLE.com/a");

System.out.println(uri1.compareTo(uri2)); // -18 ("example.com" < "www.example.com")
System.out.println(uri1.compareTo(uri3)); // 0 ("example.com" = "EXAMPLE.com")

final var uri4 = URI.create("http://user-a@example.com/a");
final var uri5 = URI.create("http://user-b@example.com/a");
final var uri6 = URI.create("http://user-A@example.com/a");

System.out.println(uri4.compareTo(uri5)); // -1 ("user-a" < "user-b")
System.out.println(uri4.compareTo(uri6)); // 32 ("user-a" > "user-A")

final var uri7 = URI.create("http://user-a@example.com:80/a");
final var uri8 = URI.create("http://user-a@example.com:8080/a");

System.out.println(uri7.compareTo(uri8)); // -8000 (80 < 8080)
// Escape.
final var uri1 = new URI("http", "example.com", "/a b c", null);
System.out.println(uri1.toASCIIString()); // "http://example.com/a%20b%20c"
System.out.println(uri1.getPath()); // "//a b c"
System.out.println(uri1.getRawPath()); // "/a%20b%20c"

final var uri2 = URI.create("http://example.com/a%20b%20c");
System.out.println(uri2.toASCIIString()); // "http://example.com/a%20b%20c"
System.out.println(uri2.getPath()); // "/a b c"
System.out.println(uri2.getRawPath()); // "/a%20b%20c"

System.out.println(uri1.compareTo(uri2)); // 0

final var uri3 = new URI("http", "example.com", "/♪", null);
System.out.println(uri3.toASCIIString()); // "http://example.com/%E2%99%AA"
System.out.println(uri3.getPath()); // "/♪"
System.out.println(uri3.getRawPath()); // "/♪"

final var uri4 = URI.create("http://example.com/%E2%99%AA");
System.out.println(uri4.toASCIIString()); // "http://example.com/%E2%99%AA"
System.out.println(uri4.getPath()); // "/♪"
System.out.println(uri4.getRawPath()); // "/%E2%99%AA"

System.out.println(uri3.compareTo(uri4)); // 9797

static URI create (String str)

Creates a URI by parsing the given string.

final var ret = URI.create("");
System.out.println(ret); // ""
// Hierarchical URIs. (absolute URIs)
final var ret1 = URI.create("http://example.com/languages/java/");
System.out.println(ret1); // "http://example.com/languages/java/"

final var ret2 = URI.create("file:///~/calendar");
System.out.println(ret2); // "file:///~/calendar"
// Hierarchical URIs. (relative URIs)
final var ret1 = URI.create("sample/a/index.html#28");
System.out.println(ret1); // "sample/a/index.html#28"

final var ret2 = URI.create("../../demo/b/index.html");
System.out.println(ret2); // "../../demo/b/index.html"
// Opaque URIs.
final var ret1 = URI.create("mailto:java-net@www.example.com");
System.out.println(ret1); // "mailto:java-net@www.example.com"

final var ret2 = URI.create("news:comp.lang.java");
System.out.println(ret2); // "news:comp.lang.java"

final var ret3 = URI.create("urn:isbn:096139210x");
System.out.println(ret3); // "urn:isbn:096139210x"
// Escape.
final var uri1 = URI.create("http://example.com/♪");
System.out.println(uri1); // "http://example.com/♪"
System.out.println(uri1.toASCIIString()); // "http://example.com/%E2%99%AA"
System.out.println(uri1.getRawPath()); // "/♪"
System.out.println(uri1.getPath()); // "/♪"

final var uri2 = URI.create("http://example.com/%E2%99%AA");
System.out.println(uri2); // "http://example.com/%E2%99%AA"
System.out.println(uri2.toASCIIString()); // "http://example.com/%E2%99%AA"
System.out.println(uri2.getRawPath()); // "/%E2%99%AA"
System.out.println(uri2.getPath()); // "/♪"
try {
    final var uri = URI.create("http:");
} catch (IllegalArgumentException e) {
    System.out.println("IllegalArgumentException! : " + e.getMessage());
}

// Result
// ↓
//IllegalArgumentException! : Expected scheme-specific part at index 5: http:

boolean equals (Object ob)

Tests this URI for equality with another object.

final var uri1 = URI.create("http://example.com/a");
final var uri2 = URI.create("http://example.com/a");
final var uri3 = URI.create("http://example.com/A");

System.out.println(uri1.equals(uri2)); // true ("a" = "a")
System.out.println(uri1.equals(uri3)); // false ("a" != "A")
final var uri1 = URI.create("http://example.com/a?b");
final var uri2 = URI.create("http://example.com/a?c");
final var uri3 = URI.create("http://example.com/a?B");

System.out.println(uri1.equals(uri2)); // false ("a?b" != "a?c")
System.out.println(uri1.equals(uri3)); // false ("a?b" != "a?B")

final var uri4 = URI.create("http://example.com/a#b");
final var uri5 = URI.create("http://example.com/a#c");
final var uri6 = URI.create("http://example.com/a#B");

System.out.println(uri4.equals(uri5)); // false ("a#b" != "a#c")
System.out.println(uri4.equals(uri6)); // false ("a#b" != "a#B")

final var uri7 = URI.create("http://example.com/a?b#b");
final var uri8 = URI.create("http://example.com/a?b#c");
final var uri9 = URI.create("http://example.com/a?b#B");

System.out.println(uri7.equals(uri8)); // false ("a?b#b" != "a?b#c")
System.out.println(uri7.equals(uri9)); // false ("a?b#b" != "a?b#B")
// Two URIs with schemes are compared case-insensitively.
final var uri1 = URI.create("http://example.com/a");

final var uri2 = URI.create("https://example.com/a");
final var uri3 = URI.create("HTTP://example.com/a");
final var uri4 = URI.create("HTTPS://example.com/a");
final var uri5 = URI.create("aaaa://example.com/a");

System.out.println(uri1.equals(uri2)); // false ("http" != "https")
System.out.println(uri1.equals(uri3)); // true ("http" = "HTTP")
System.out.println(uri1.equals(uri4)); // false ("http" != "HTTPS")
System.out.println(uri1.equals(uri5)); // false ("http" != "aaaa")
final var uri1 = URI.create("http://example.com");

final var uri2 = URI.create("http:example.com");
final var uri3 = URI.create("http:www.example.com");

System.out.println(uri1.equals(uri2)); // false
System.out.println(uri1.equals(uri3)); // false
// Opaque URIs with an identical scheme.
final var uri1 = URI.create("mailto:aaa@example.com");
final var uri2 = URI.create("mailto:bbb@example.com");
final var uri3 = URI.create("mailto:AAA@example.com");

System.out.println(uri1.equals(uri2)); // false ("aaa" != "bbb")
System.out.println(uri1.equals(uri3)); // false ("aaa" != "AAA")

final var uri4 = URI.create("mailto:aaa@example.com#xxx");
final var uri5 = URI.create("mailto:aaa@example.com#yyy");

System.out.println(uri1.equals(uri4)); // false (null != "xxx")
System.out.println(uri1.equals(uri5)); // false (null != "yyy")
System.out.println(uri5.equals(uri4)); // false ("yyy" != "xxx")
// Two URIs with hosts are compared case-insensitively.
final var uri1 = URI.create("http://example.com/a");
final var uri2 = URI.create("http://www.example.com/a");
final var uri3 = URI.create("http://EXAMPLE.com/a");

System.out.println(uri1.equals(uri2)); // false ("example.com" != "www.example.com")
System.out.println(uri1.equals(uri3)); // true ("example.com" = "EXAMPLE.com")

final var uri4 = URI.create("http://user-a@example.com/a");
final var uri5 = URI.create("http://user-b@example.com/a");
final var uri6 = URI.create("http://user-A@example.com/a");

System.out.println(uri4.equals(uri5)); // false ("user-a" != "user-b")
System.out.println(uri4.equals(uri6)); // false ("user-a" != "user-A")

final var uri7 = URI.create("http://user-a@example.com:80/a");
final var uri8 = URI.create("http://user-a@example.com:8080/a");

System.out.println(uri7.equals(uri8)); // false (80 != 8080)
// Escape.
final var uri1 = new URI("http", "example.com", "/a b c", null);
System.out.println(uri1.toASCIIString()); // "http://example.com/a%20b%20c"
System.out.println(uri1.getPath()); // "//a b c"
System.out.println(uri1.getRawPath()); // "/a%20b%20c"

final var uri2 = URI.create("http://example.com/a%20b%20c");
System.out.println(uri2.toASCIIString()); // "http://example.com/a%20b%20c"
System.out.println(uri2.getPath()); // "/a b c"
System.out.println(uri2.getRawPath()); // "/a%20b%20c"

System.out.println(uri1.equals(uri2)); // true

final var uri3 = new URI("http", "example.com", "/♪", null);
System.out.println(uri3.toASCIIString()); // "http://example.com/%E2%99%AA"
System.out.println(uri3.getPath()); // "/♪"
System.out.println(uri3.getRawPath()); // "/♪"

final var uri4 = URI.create("http://example.com/%E2%99%AA");
System.out.println(uri4.toASCIIString()); // "http://example.com/%E2%99%AA"
System.out.println(uri4.getPath()); // "/♪"
System.out.println(uri4.getRawPath()); // "/%E2%99%AA"

System.out.println(uri3.equals(uri4)); // false
final var s = "http://example.com/a";
final var uri = URI.create(s);
System.out.println(uri.equals(s)); // false

String getAuthority ()

Returns the decoded authority component of this URI.

final var uri1 = URI.create("http://user@example.com:80/");
System.out.println(uri1.getAuthority()); // "user@example.com:80"
System.out.println(uri1.getRawAuthority()); // "user@example.com:80"
System.out.println(uri1.getHost()); // "example.com"

final var uri2 = URI.create("mailto:java-net@www.example.com");
System.out.println(uri2.getAuthority()); // null
System.out.println(uri2.getRawAuthority()); // null
System.out.println(uri2.getHost()); // null

final var uri3 = new URI("http", "us er", "example.com", -1, null, null, null);
System.out.println(uri3.getAuthority()); // "us er@example.com"
System.out.println(uri3.getRawAuthority()); // "us%20er@example.com"
System.out.println(uri3.getHost()); // "example.com"

String getFragment ()

Returns the decoded fragment component of this URI.

final var uri1 = URI.create("http://example.com/path#abc");
System.out.println(uri1.getFragment()); // "abc"
System.out.println(uri1.getRawFragment()); // "abc"

final var uri2 = URI.create("http://example.com/path");
System.out.println(uri2.getFragment()); // null
System.out.println(uri2.getRawFragment()); // null

final var uri3 = new URI("http", "example.com", "/path", null, "a b c");
System.out.println(uri3); // "http://example.com/path#a%20b%20c"
System.out.println(uri3.getFragment()); // "a b c"
System.out.println(uri3.getRawFragment()); // "a%20b%20c"

String getHost ()

Returns the host component of this URI.

final var uri1 = URI.create("http://user@example.com:80/");
System.out.println(uri1.getHost()); // "example.com"
System.out.println(uri1.getAuthority()); // "user@example.com:80"

final var uri2 = URI.create("mailto:java-net@www.example.com");
System.out.println(uri2.getHost()); // null
System.out.println(uri2.getAuthority()); // null

String getPath ()

Returns the decoded path component of this URI.

final var uri1 = URI.create("http://example.com/path/aaa/index.html");
System.out.println(uri1.getPath()); // "/path/aaa/index.html"
System.out.println(uri1.getRawPath()); // "/path/aaa/index.html"

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.getPath()); // null
System.out.println(uri2.getRawPath()); // null

final var uri3 = new URI("http", "example.com", "/pa th", null);
System.out.println(uri3); // "http://example.com/pa%20th"
System.out.println(uri3.getPath()); // "/pa th"
System.out.println(uri3.getRawPath()); // "/pa%20th"

int getPort ()

Returns the port number of this URI.

final var uri1 = URI.create("http://example.com:80/path");
System.out.println(uri1.getPort()); // 80

final var uri2 = URI.create("http://example.com/path");
System.out.println(uri2.getPort()); // -1

String getQuery ()

Returns the decoded query component of this URI.

final var uri1 = URI.create("http://example.com/path?query");
System.out.println(uri1.getQuery()); // "query"
System.out.println(uri1.getRawQuery()); // "query"

final var uri2 = URI.create("http://example.com/path");
System.out.println(uri2.getQuery()); // null
System.out.println(uri2.getRawQuery()); // null

final var uri3 = new URI("http", null, "example.com", -1, "/path", "qu ery", null);
System.out.println(uri3); // "http://example.com/path?qu%20ery"
System.out.println(uri3.getQuery()); // "qu ery"
System.out.println(uri3.getRawQuery()); // "qu%20ery"

String getRawAuthority ()

Returns the raw authority component of this URI.

final var uri1 = URI.create("http://user@example.com:80/");
System.out.println(uri1.getAuthority()); // "user@example.com:80"
System.out.println(uri1.getRawAuthority()); // "user@example.com:80"
System.out.println(uri1.getHost()); // "example.com"

final var uri2 = URI.create("mailto:java-net@www.example.com");
System.out.println(uri2.getAuthority()); // null
System.out.println(uri2.getRawAuthority()); // null
System.out.println(uri2.getHost()); // null

final var uri3 = new URI("http", "us er", "example.com", -1, null, null, null);
System.out.println(uri3.getAuthority()); // "us er@example.com"
System.out.println(uri3.getRawAuthority()); // "us%20er@example.com"
System.out.println(uri3.getHost()); // "example.com"

String getRawFragment ()

Returns the raw fragment component of this URI.

final var uri1 = URI.create("http://example.com/path#abc");
System.out.println(uri1.getFragment()); // "abc"
System.out.println(uri1.getRawFragment()); // "abc"

final var uri2 = URI.create("http://example.com/path");
System.out.println(uri2.getFragment()); // null
System.out.println(uri2.getRawFragment()); // null

final var uri3 = new URI("http", "example.com", "/path", null, "a b c");
System.out.println(uri3); // "http://example.com/path#a%20b%20c"
System.out.println(uri3.getFragment()); // "a b c"
System.out.println(uri3.getRawFragment()); // "a%20b%20c"

String getRawPath ()

Returns the raw path component of this URI.

final var uri1 = URI.create("http://example.com/path/aaa/index.html");
System.out.println(uri1.getPath()); // "/path/aaa/index.html"
System.out.println(uri1.getRawPath()); // "/path/aaa/index.html"

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.getPath()); // null
System.out.println(uri2.getRawPath()); // null

final var uri3 = new URI("http", "example.com", "/pa th", null);
System.out.println(uri3); // "http://example.com/pa%20th"
System.out.println(uri3.getPath()); // "/pa th"
System.out.println(uri3.getRawPath()); // "/pa%20th"

String getRawQuery ()

Returns the raw query component of this URI.

final var uri1 = URI.create("http://example.com/path?query");
System.out.println(uri1.getQuery()); // "query"
System.out.println(uri1.getRawQuery()); // "query"

final var uri2 = URI.create("http://example.com/path");
System.out.println(uri2.getQuery()); // null
System.out.println(uri2.getRawQuery()); // null

final var uri3 = new URI("http", null, "example.com", -1, "/path", "qu ery", null);
System.out.println(uri3); // "http://example.com/path?qu%20ery"
System.out.println(uri3.getQuery()); // "qu ery"
System.out.println(uri3.getRawQuery()); // "qu%20ery"

String getRawSchemeSpecificPart ()

Returns the raw scheme-specific part of this URI.

final var uri1 = URI.create("http://user@example.com:80/path?query#fragment");
System.out.println(uri1.getSchemeSpecificPart()); // "//user@example.com:80/path?query"
System.out.println(uri1.getRawSchemeSpecificPart()); // "//user@example.com:80/path?query"

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.getSchemeSpecificPart()); // "comp.lang.java"
System.out.println(uri2.getRawSchemeSpecificPart()); // "comp.lang.java"

final var uri3 = URI.create("#fragment");
System.out.println(uri3.getSchemeSpecificPart()); // ""
System.out.println(uri3.getRawSchemeSpecificPart()); // ""

final var uri4 = new URI("http", "us er", "example.com", 80, "/path", "query", "fragment");
System.out.println(uri4); // "http://us%20er@example.com:80/path?query#fragment"
System.out.println(uri4.getSchemeSpecificPart()); // "//us er@example.com:80/path?query"
System.out.println(uri4.getRawSchemeSpecificPart()); // "//us%20er@example.com:80/path?query"

String getRawUserInfo ()

Returns the raw user-information component of this URI.

final var uri1 = URI.create("http://user@example.com:80/path");
System.out.println(uri1.getUserInfo()); // "user"
System.out.println(uri1.getRawUserInfo()); // "user"

final var uri2 = URI.create("http://example.com/path");
System.out.println(uri2.getUserInfo()); // null
System.out.println(uri2.getRawUserInfo()); // null

final var uri3 = new URI("http", "us er", "example.com", 80, "/path", null, null);
System.out.println(uri3); // "http://us%20er@example.com:80/path"
System.out.println(uri3.getUserInfo()); // "us er"
System.out.println(uri3.getRawUserInfo()); // "us%20er"

String getScheme ()

Returns the scheme component of this URI.

final var uri1 = URI.create("http://example.com/path");
System.out.println(uri1.getScheme()); // "http"

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.getScheme()); // "news"

final var uri3 = URI.create("urn:isbn:096139210x");
System.out.println(uri3.getScheme()); // "urn"

final var uri4 = URI.create("../../demo/b/index.html");
System.out.println(uri4.getScheme()); // null

String getSchemeSpecificPart ()

Returns the decoded scheme-specific part of this URI.

final var uri1 = URI.create("http://user@example.com:80/path?query#fragment");
System.out.println(uri1.getSchemeSpecificPart()); // "//user@example.com:80/path?query"
System.out.println(uri1.getRawSchemeSpecificPart()); // "//user@example.com:80/path?query"

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.getSchemeSpecificPart()); // "comp.lang.java"
System.out.println(uri2.getRawSchemeSpecificPart()); // "comp.lang.java"

final var uri3 = URI.create("#fragment");
System.out.println(uri3.getSchemeSpecificPart()); // ""
System.out.println(uri3.getRawSchemeSpecificPart()); // ""

final var uri4 = new URI("http", "us er", "example.com", 80, "/path", "query", "fragment");
System.out.println(uri4); // "http://us%20er@example.com:80/path?query#fragment"
System.out.println(uri4.getSchemeSpecificPart()); // "//us er@example.com:80/path?query"
System.out.println(uri4.getRawSchemeSpecificPart()); // "//us%20er@example.com:80/path?query"

String getUserInfo ()

Returns the decoded user-information component of this URI.

final var uri1 = URI.create("http://user@example.com:80/path");
System.out.println(uri1.getUserInfo()); // "user"
System.out.println(uri1.getRawUserInfo()); // "user"

final var uri2 = URI.create("http://example.com/path");
System.out.println(uri2.getUserInfo()); // null
System.out.println(uri2.getRawUserInfo()); // null

final var uri3 = new URI("http", "us er", "example.com", 80, "/path", null, null);
System.out.println(uri3); // "http://us%20er@example.com:80/path"
System.out.println(uri3.getUserInfo()); // "us er"
System.out.println(uri3.getRawUserInfo()); // "us%20er"

int hashCode ()

Returns a hash-code value for this URI.

final var uri1 = URI.create("http://example.com/a");
System.out.println(uri1.hashCode()); // 1387131798

final var uri2 = URI.create("https://example.com/a");
System.out.println(uri2.hashCode()); // 1712578137

final var uri3 = URI.create("HTTP://example.com/a");
System.out.println(uri3.hashCode()); // 1387131798

boolean isAbsolute ()

Tells whether or not this URI is absolute.

// Hierarchical URIs. (absolute URIs)
final var uri1 = URI.create("http://example.com/languages/java/");
System.out.println(uri1.isAbsolute()); // true

final var uri2 = URI.create("file:///~/calendar");
System.out.println(uri2.isAbsolute()); // true
// Hierarchical URIs. (relative URIs)
final var uri1 = URI.create("sample/a/index.html#28");
System.out.println(uri1.isAbsolute()); // false

final var uri2 = URI.create("../../demo/b/index.html");
System.out.println(uri2.isAbsolute()); // false
// Opaque URIs.
final var uri1 = URI.create("mailto:java-net@www.example.com");
System.out.println(uri1.isAbsolute()); // true

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.isAbsolute()); // true

boolean isOpaque ()

Tells whether or not this URI is opaque.

// Hierarchical URIs. (absolute URIs)
final var uri1 = URI.create("http://example.com/languages/java/");
System.out.println(uri1.isOpaque()); // false

final var uri2 = URI.create("file:///~/calendar");
System.out.println(uri2.isOpaque()); // false
// Hierarchical URIs. (relative URIs)
final var uri1 = URI.create("sample/a/index.html#28");
System.out.println(uri1.isOpaque()); // false

final var uri2 = URI.create("../../demo/b/index.html");
System.out.println(uri2.isOpaque()); // false
// Opaque URIs.
final var uri1 = URI.create("mailto:java-net@www.example.com");
System.out.println(uri1.isOpaque()); // true

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.isOpaque()); // true

URI normalize ()

Normalizes this URI's path.

final var uri1 = URI.create("http://example.com/languages/java/");
System.out.println(uri1.normalize()); // "http://example.com/languages/java/"

final var uri2 = URI.create("http://example.com/languages/../java/");
System.out.println(uri2.normalize()); // "http://example.com/java/"

final var uri3 = URI.create("http://example.com/languages/./java/");
System.out.println(uri3.normalize()); // "http://example.com/languages/java/"

final var uri4 = URI.create("mailto:java-net@www.example.com");
System.out.println(uri4.normalize()); // "mailto:java-net@www.example.com"

final var uri5 = URI.create("sample/a/index.html#28");
System.out.println(uri5.normalize()); // "sample/a/index.html#28"

final var uri6 = URI.create("sample/../a/index.html#28");
System.out.println(uri6.normalize()); // "a/index.html#28"

final var uri7 = URI.create("./sample/a/index.html#28");
System.out.println(uri7.normalize()); // "sample/a/index.html#28"

final var uri8 = URI.create("../../demo/b/index.html");
System.out.println(uri8.normalize()); // "../../demo/b/index.html"

URI parseServerAuthority ()

Attempts to parse this URI's authority component, if defined, into user-information, host, and port components.

final var uri1 = URI.create("http://example.com/languages/java/");
System.out.println(uri1.parseServerAuthority()); // "http://example.com/languages/java/"

final var uri2 = URI.create("mailto:java-net@www.example.com");
System.out.println(uri2.parseServerAuthority()); // "mailto:java-net@www.example.com"

final var uri3 = URI.create("sample/a/index.html#28");
System.out.println(uri3.parseServerAuthority()); // "sample/a/index.html#28"
final var uri = URI.create("//foo:bar");
try {
    final var ret = uri.parseServerAuthority();
} catch (URISyntaxException e) {
    System.out.println("URISyntaxException! : " + e.getMessage());
}

// Result
// ↓
//URISyntaxException! : Illegal character in port number at index 6: //foo:bar

URI relativize (URI uri)

Relativizes the given URI against this URI.

final var uri1 = URI.create("http://example.com/");
final var uri2 = URI.create("http://example.com/aaa");
final var uri3 = URI.create("http://example.com/aaa/bbb");

System.out.println(uri1.relativize(uri2)); // "aaa"
System.out.println(uri1.relativize(uri3)); // "aaa/bbb"

System.out.println(uri2.relativize(uri1)); // "http://example.com/"
System.out.println(uri2.relativize(uri3)); // "bbb"

System.out.println(uri3.relativize(uri1)); // "http://example.com/"
System.out.println(uri3.relativize(uri2)); // "http://example.com/aaa"
final var uri1 = URI.create("http://example.com/");
final var uri2 = URI.create("http://example.com/aaa?query#fragment");

System.out.println(uri1.relativize(uri2)); // "aaa?query#fragment"
System.out.println(uri2.relativize(uri1)); // "http://example.com/"
final var uri1 = URI.create("http://example.com/");
final var uri2 = URI.create("https://example.com/aaa");
final var uri3 = URI.create("mailto:java-net@www.example.com");
final var uri4 = URI.create("//foo:bar");

System.out.println(uri1.relativize(uri2)); // "https://example.com/aaa"
System.out.println(uri1.relativize(uri3)); // "mailto:java-net@www.example.com"
System.out.println(uri1.relativize(uri4)); // "//foo:bar"
final var uri1 = URI.create("http://example.com/");
final var uri2 = URI.create("http://user@example.com:80/aaa");

System.out.println(uri1.relativize(uri2)); // "http://user@example.com:80/aaa"
System.out.println(uri2.relativize(uri1)); // "http://example.com/"

URI resolve (String str)

Constructs a new URI by parsing the given string and then resolving it against this URI.

This method works as if invoking it were equivalent to evaluating the expression URI.resolve(URI.create(str)).

URI resolve (URI uri)

Resolves the given URI against this URI.

final var uri1 = URI.create("http://example.com/languages/java/");
final var uri2 = URI.create("sample/a/index.html#28");
final var uri3 = URI.create("../../demo/b/index.html");

System.out.println(uri1.resolve(uri2)); // "http://example.com/languages/java/sample/a/index.html#28"
System.out.println(uri1.resolve(uri3)); // "http://example.com/demo/b/index.html"
System.out.println(uri1.resolve(uri2).resolve(uri3)); // "http://example.com/languages/java/demo/b/index.html"

System.out.println(uri2.resolve(uri1)); // "http://example.com/languages/java/"
System.out.println(uri2.resolve(uri3)); // "demo/b/index.html"

System.out.println(uri3.resolve(uri1)); // "http://example.com/languages/java/"
System.out.println(uri3.resolve(uri2)); // "../../demo/b/sample/a/index.html#28"
final var uri1 = URI.create("http://example.com/");
System.out.println(uri1); // "http://example.com/"

final var uri2 = uri1.resolve(URI.create("aaa/"));
System.out.println(uri2);  // "http://example.com/aaa/"

final var uri3 = uri2.resolve(URI.create("bbb/"));
System.out.println(uri3); // "http://example.com/aaa/bbb/"

final var uri4 = uri3.resolve(URI.create("index.html?uuu#vvv"));
System.out.println(uri4); // "http://example.com/aaa/bbb/index.html?uuu#vvv"

final var uri5 = uri4.resolve(URI.create("index.html?www#xxx"));
System.out.println(uri5); // "http://example.com/aaa/bbb/index.html?www#xxx"
final var uri1 = URI.create("");
System.out.println(uri1); // ""

final var uri2 = uri1.resolve(URI.create("aaa/"));
System.out.println(uri2); // "aaa/"

final var uri3 = uri2.resolve(URI.create("bbb/"));
System.out.println(uri3); // "aaa/bbb/"

final var uri4 = uri3.resolve(URI.create("index.html?uuu#vvv"));
System.out.println(uri4); // "aaa/bbb/index.html?uuu#vvv"

final var uri5 = uri4.resolve(URI.create("index.html?www#xxx"));
System.out.println(uri5); // "aaa/bbb/index.html?www#xxx"
final var uri1 = URI.create("http://example.com/aaa.index");
final var uri2 = new URI(null, "www.example.com", "/bbb.index", null);
System.out.println(uri2); // "//www.example.com/bbb.index"

System.out.println(uri1.resolve(uri2)); // "http://www.example.com/bbb.index"
System.out.println(uri2.resolve(uri1)); // "http://example.com/aaa.index"
final var uri1 = URI.create("http://example.com/");
final var uri2 = URI.create("mailto:java-net@www.example.com");

System.out.println(uri1.resolve(uri2)); // "mailto:java-net@www.example.com"
System.out.println(uri2.resolve(uri1)); // "http://example.com/"
final var uri1 = URI.create("http://example.com/index.html");
final var uri2 = URI.create("#foo");

System.out.println(uri1.resolve(uri2)); // "http://example.com/index.html#foo"

String toASCIIString ()

Returns the content of this URI as a US-ASCII string.

final var uri1 = URI.create("http://example.com/languages/java/");
System.out.println(uri1.toString()); // "http://example.com/languages/java/"
System.out.println(uri1.toASCIIString()); // "http://example.com/languages/java/"

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.toString()); // "news:comp.lang.java"
System.out.println(uri2.toASCIIString()); // "news:comp.lang.java"

final var uri3 = URI.create("http://example.com/♪");
System.out.println(uri3.toString()); // "http://example.com/♪"
System.out.println(uri3.toASCIIString());  // "http://example.com/%E2%99%AA"

final var uri4 = new URI("http", "example.com", "/a b c", null);
System.out.println(uri4.toString()); // "http://example.com/a%20b%20c"
System.out.println(uri4.toASCIIString()); // "http://example.com/a%20b%20c"

String toString ()

Returns the content of this URI as a string.

final var uri1 = URI.create("http://example.com/languages/java/");
System.out.println(uri1.toString()); // "http://example.com/languages/java/"
System.out.println(uri1.toASCIIString()); // "http://example.com/languages/java/"

final var uri2 = URI.create("news:comp.lang.java");
System.out.println(uri2.toString()); // "news:comp.lang.java"
System.out.println(uri2.toASCIIString()); // "news:comp.lang.java"

final var uri3 = URI.create("http://example.com/♪");
System.out.println(uri3.toString()); // "http://example.com/♪"
System.out.println(uri3.toASCIIString());  // "http://example.com/%E2%99%AA"

final var uri4 = new URI("http", "example.com", "/a b c", null);
System.out.println(uri4.toString()); // "http://example.com/a%20b%20c"
System.out.println(uri4.toASCIIString()); // "http://example.com/a%20b%20c"

URL toURL ()

Constructs a URL from this URI.

final var uri = URI.create("http://example.com/languages/java/");
System.out.println(uri.toURL()); // "http://example.com/languages/java/"
final var uri = URI.create("../../demo/b/index.html");
try {
    final var ret = uri.toURL();
} catch (IllegalArgumentException e) {
    System.out.println("IllegalArgumentException! : " + e.getMessage());
}

// Result
// ↓
//IllegalArgumentException! : URI is not absolute
final var uri = URI.create("news:comp.lang.java");
try {
    final var ret = uri.toURL();
} catch (MalformedURLException e) {
    System.out.println("MalformedURLException! : " + e.getMessage());
}

// Result
// ↓
//MalformedURLException! : unknown protocol: news

Related posts

To top of page