0
votes

Why does URI allow missing protocol (while URL does not)?

In wikipedia Scheme (and even Path) seem to be obligatory components of an URI:

The URI generic syntax consists of a hierarchical sequence of five components:[8]

URI = scheme:[//authority]path[?query][#fragment]

Or missing protocol defaults to something (like http)? I found nothing like this in the docs.

new URI("my.html");        // 1
new URI("xabc:my.html");   // 2
new URL("my.html");        // 3      
new URL("xabc:my.html");   // 4

Concerning "obligatory" path - OK, there is oblique URI. But why missing protocol is allowed (it shall be present even for obligue URI which is required to be absolute)

I could understand that relative URL/URI don't require protocol (<img src="/images/pic.png">), but URL gives run-time java.net.MalformedURLException: no protocol in this case either (while URI don't).

1
Show code that throw MalformedURLException exceptionuser7294900

1 Answers

1
votes

Your relative path must be wrong, Java's URI supports empty scheme for relative URI:

relative URI, that is, a URI that does not specify a scheme. Some examples of hierarchical URIs are:

docs/guide/collections/designfaq.html#28

Scheme is optional:

[scheme:]scheme-specific-part[#fragment]

Similar with URL, e.g.:

URL url = new URL("/guidelines.txt");