0

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 exception Commented Jan 7, 2019 at 9:57

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.