1
votes

URL constructor does not allow missing or unknown protocol.

But this does not support widely-used relative URLs like

  1. Root-relative URL <img src="/images/pic.png>
  2. Page/Base-relative URL <img src="images/pic.png>

Why URL constructors throws java.net.MalformedURLException: no protocol in these very common cases?

URI constructor supports such relative URLs.

By the way, is it correct to say "relative URI" instead of "relative URL"? Because 99% all links in the Internet is "relative URL", not "relative URI"

3
Look at documentation for java.net.URL and java.net.URI. - Martin Heralecký
yeah, good to see it. Knowledge :) - Vishwa Ratna

3 Answers

1
votes

Why URL constructors throws java.net.MalformedURLException: no protocol in these very common cases?

Because these are not valid URLs.

URI constructor supports such relative URLs.

Incorrect. It allows relative URIs.

By the way, is it correct to say "relative URI" instead of "relative URL"? Because 99% all links in the Internet is "relative URL", not "relative URI".

No. It is not correct1.

The specifications for URI and URL make it clear that "relative URL" is not valid terminology. See https://www.rfc-editor.org/rfc/rfc3986

The designers of the Java APIs for URI and URL chose (wisely IMO) to use the terminology correctly; i.e. according to the respective specifications. That is why the URL constructor will not accept a relative URI string as an argument.


1 - It is no more correct than it is correct to refer to ants as "bugs". The true bugs are the Hemiptera which is an order of Insecta (the insects) that is distinguished by having sucking mouth parts. The ants are members of the Hymenoptera order. As Wikipedia states: "Many insects commonly known as "bugs" belong to other orders; for example, the lovebug is a fly, while the May bug and ladybug are beetles." (see Wikipedia) My point? If the Java designers abused the URL / URI distinction, they would be just as bad as a biologist who referred to ants as "bugs" in a scientific paper.

0
votes

Why URL constructors throws java.net.MalformedURLException: no protocol in these very common cases?

According to documentation:

Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.

Example 1: "https://stackoverflow.com/something/java/something" -> It will throw error

Example 1.1: https://stackoverflow.com/something/java/something -> It will not throw error.

In Simple sense it means-> Example 1: is not parsed inside the string., so remove "".

URIs identify and URLs locate; however, locators are also identifiers, so every URL is also a URI, but there are URIs which are not URLs.

A exposed Venn diagram: ( URIs ( URLs ) )

above Venn Diagram Implies: URL is more specific than URI.

0
votes

A URL is composed of different parts like protocol , host and resource present on that host.

A relative URI does not contains all the information for creating a URL so it throws java.net.MalformedURLException.