Search code examples
javalinuxurifile-uri

Why would aFile.toURI().toString() on linux have a single slash?


What is the deal with Java's bizarre file protocol handling? I mean on windows UNC path's get turned into 5 slashes, and I get why that happens, but on linux an absolute path gets turned into file:/local/path/to/file Shouldn't that have three slashes? I'm assuming the authors of Java aren't incompetent, so is there an explanation for why that's acceptable?


Solution

  • Let’s start with the documentation of the URI class:

    A hierarchical URI is subject to further parsing according to the syntax

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

    As you can see, the authority is optional. This is supported by the URI specification, section 3:

    The scheme and path components are required, though the path may be empty (no characters). When authority is present, the path must either be empty or begin with a slash ("/") character. When authority is not present, the path cannot begin with two slash characters ("//").

    A file: URI can have an authority separator, //, with an effectively empty authority after it, but it serves no purpose, so there is no harm in omitting it. It’s still a fully compliant URI.