• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URI

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I saw this code in OCPJP SE 8 preparation book written by Boyarsky and Selikoff. I don't understand why the first one throws an exception but the third one doesn't. I tried running it through my compiler but no RuntimeException was thrown. I am really confused. What am I missing?
 
author & internet detective
Posts: 41855
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying the first one doesn't throw an exception on your machine? It does on a Mac:

Exception in thread "main" java.lang.IllegalArgumentException: URI has an authority component
at sun.nio.fs.UnixUriUtils.fromUri(UnixUriUtils.java:53)
at sun.nio.fs.UnixFileSystemProvider.getPath(UnixFileSystemProvider.java:98)
at java.nio.file.Paths.get(Paths.java:138)


I'm guessing you are on Windows?

In any case, it is wrong because a URI needs to be an absolute reference so Java can find it.
 
Saloon Keeper
Posts: 27720
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, URI "file://pandas/cuddly.org" should properly be referring to a relative file path. Subdirectory panda, filename, "cuddly.org".

For reasons left obscure, the syntax of a URL should conform to the following pattern:

<scheme>:[//<authority>][<path>][?<query>]

For example:

https://coderanch.com/context/etc

Jean's example seems to have parsed improperly and it's taking the "//" of the authority component as though it were the "//" which is the translated version of the "\\" of a CIFS UNC name.

If my memory is accurate, this would be the proper encoding for a UNC filepath:

file:////server/share/directory1/directory2/filename.ext

Two slashes for the authority marker, 2 to represent the UNC path \\server\share/directory1/directory2/filename.ext. Note that the URL

file://\\server\share\directory1\directory2\filename.ext is technically legal, but you can burn yourself using it, since in Java you have to double-up on backslashes when coding string literals to avoid getting zapped by the escape mechanism:

file://\\\\server\\share/directory1/directory2/filename.ext and other variations on this theme,

Likewise, for local relative file paths:

file://directory1/directory2/filename.ext

And absolute paths:

file:///basedirectory/directory1/filename.ext

For systems with a single root, such as Unix, Linux, and MacOS

And

file:///c:/basedirectory/directory1/filename.ext

for multi-rooted filesystems like Windows (there are variants on this syntax, but this is one of the cleanest).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic