• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Path.ToRealPath method

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I saw this test in Sierra/Bates study guide:

Having a directory x under c: drive what is the result of System.out.println(Paths.get("c:/x/z/..").toRealPath()); ?

I answered c:\x but according to the solution this is wrong. Correct solution should be: throws NoSuchFileException. The toRealPath() function actually looks at the filesystem. Since there is no directory named z an exception is thrown.

I thought that path was normalized removing directory z, but according the solution this should not be done.

However If i try
System.out.println(Paths.get("c:/users/BONPAS00/Documents/XXXXX/..").toRealPath());

the output is C:\Users\BONPAS00\Documents.

So I don't underdsand the exact behavior.

Thanks
 
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I tried it and got that Exception when there were tiny spelling errors in Paths. This is what it says in the Path class.
 
Pasquale Bonito
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked at javadoc as well and it's clear thatt .. removes previous dir. It seems that the exercise in Sierra/Bates is wrong.

thanks
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Sierra and Bates appears to be correct. They said it would throw an Exception.
 
Pasquale Bonito
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is not about if that methods throws Exception or not but if it throws Exception in a specific case.

What happens if you create a dir "x" with no child under c:\ and than run a program with this line of code ? System.out.println(Paths.get("c:/x/z/..").toRealPath());
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it. I did. I suffered an Exception. I used .../references and all was well. I used .../References (which is wrong) and there was a NoSuchFileException. The Path#toRealPath method declares an IOException, however.
 
Pasquale Bonito
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it as well, and I didn't catch any exception since directory c:\x exists and path is normalized deleting ../y before looking at the real path.

I agree thta if the path does not exists on filesystem you hava an exceptio.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure it was the double dot in the book for parent directory and not a treble dot (=ellipsis=…)?

You are right that the double dot will remove the z directory and I hasn't noticed that, so I was mistaken there. Sorry.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect such things may be platform‑dependent, but I tried the following paths on a Linux box:-
/home/campbell/Documents/lectures/Forth/References/../references
/home/campbell/Documents/lectures/Forth/references/../references

References does not exists, but references does. Tryint the first path even with .. throws an Exception, so it appears that .. only normalises a Path if whatever precedes the .. exists. Please check, but it still appears Sierra and Bates are correct there.
 
Saloon Keeper
Posts: 28656
211
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

Pasquale Bonito wrote:The question is not about if that methods throws Exception or not but if it throws Exception in a specific case.

What happens if you create a dir "x" with no child under c:\ and than run a program with this line of code ? System.out.println(Paths.get("c:/x/z/..").toRealPath());



Although the exact behavior of the Path methods will vary with the platform, referencing a non-existing element is almost guaranteed to fail.

The reason being that one of the things that getRealPath() will do unless otherwise instructed is resolve aliases.

So if I make a symlink /etc/tomcat/conf that equates to the real path /var/lib/apache-tomcat-6.0.28/conf, the result of the getRealPath("/etc/tomcat/conf/server.xml") would be "/var/lib/apache-tomcat-6.0.28/conf/server.xml".

Because of this potential, it's not safe to assume that "/etc/tomcat/conf/.." is really /etc/tomcat/conf if the "conf" directory doesn't exist at the moment.
reply
    Bookmark Topic Watch Topic
  • New Topic