• Post Reply Bookmark Topic Watch Topic
  • New Topic

File.renameTo() fails on Linux Ubuntu within the same file system  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am aware that I can use Files.move to rename a file in Java 7/8. But this question is specifically with java.io.File.renameTo() with Java 8...

As you can see from the code both the files are in the same mount point as well as same file system. The value status returns false.




Any thoughts on why this would happen ?
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the stack trace?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happens? Does it compile? Do you get an Exception? Do those files actually exist on the file system?
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Can you post the stack trace?


There is no stack trace. It prints false. There is no exception. Yes it does compile.
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A status of zero means no error in Linux.

Edit: never mind, the renameTo() function changes the OS status to boolean.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what happens? Does it compile? Do you get an Exception? Do those files actually exist on the file system?


a.txt does exist on the file system.


When I add this line of code



it does work.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:A status of zero means no error in Linux.


But the file did not get renamed.
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I was mistaken about what renameTo() returns. I thought it was the OS status but it's not.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Yeah, I was mistaken about what renameTo() returns. I thought it was the OS status but it's not.


So what are my options ? Use Files.move() and let this silently die. Or investigate this further ?
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you try the same rename from the Linux command prompt?
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does "/home/suraj/renamed.txt" already exist?
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:What happens if you try the same rename from the Linux command prompt?


You mean run /usr/bin/rename ?
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Does "/home/suraj/renamed.txt" already exist?



No renamed.txt does not exist.
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. I believe it fails if the file to rename to already exists.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I ran the code in my local system its working fine , but when i modify the security credential on file then i am also getting the false value ,so please check the read/write access to file .
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
devendra K Kumar wrote:Hi ,

I ran the code in my local system its working fine , but when i modify the security credential on file then i am also getting the false value ,so please check the read/write access to file .



I did a chmod 777 on a.txt and I still get a false :-)

 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try:

 
devendra K Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj sahitya wrote:
devendra K Kumar wrote:Hi ,

I ran the code in my local system its working fine , but when i modify the security credential on file then i am also getting the false value ,so please check the read/write access to file .



I did a chmod 777 on a.txt and I still get a false :-)



After doing chmod 777 , check the access parameter .

ll a.txt
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Did you try:



I did and this is what I get -

Unknown regexp modifier "/w" at (eval 1) line 1, at end of line
Unknown regexp modifier "/n" at (eval 1) line 1, at end of line
Bareword "suraj" not allowed while "strict subs" in use at (eval 1) line
Bareword "a" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "txt" not allowed while "strict subs" in use at (eval 1) line 1.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
devendra K Kumar wrote:
suraj sahitya wrote:
devendra K Kumar wrote:Hi ,

I ran the code in my local system its working fine , but when i modify the security credential on file then i am also getting the false value ,so please check the read/write access to file .



I did a chmod 777 on a.txt and I still get a false :-)



After doing chmod 777 , check the access parameter .

ll a.txt


It is r-wxrwxrwx. But still false ;-)
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like Perl.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:That looks like Perl.


So Knute what is the solution ? Is LINUX rename on Ubuntu the problem ?
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Linux is rusty. Try:

 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:My Linux is rusty. Try:



That works without an issue.

So use Files.move from Java 7/8 ? ;-)

 
devendra K Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj sahitya wrote:
devendra K Kumar wrote:
suraj sahitya wrote:
devendra K Kumar wrote:Hi ,

I ran the code in my local system its working fine , but when i modify the security credential on file then i am also getting the false value ,so please check the read/write access to file .



I did a chmod 777 on a.txt and I still get a false :-)



After doing chmod 777 , check the access parameter .

ll a.txt


It is r-wxrwxrwx. But still false ;-)


try chmod 755 or 744
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:My Linux is rusty. Try:



If you look at the code of UnixFileSystem.java from java.io this is how it looks like. You think this code calls my rename on Linux Ubuntu and fails ?
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
devendra K Kumar wrote:
suraj sahitya wrote:
devendra K Kumar wrote:
suraj sahitya wrote:
devendra K Kumar wrote:Hi ,

I ran the code in my local system its working fine , but when i modify the security credential on file then i am also getting the false value ,so please check the read/write access to file .



I did a chmod 777 on a.txt and I still get a false :-)



After doing chmod 777 , check the access parameter .

ll a.txt


It is r-wxrwxrwx. But still false ;-)


try chmod 755 or 744


Tried both and both fail :-)
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj sahitya wrote:So use Files.move from Java 7/8 ? ;-)


Yeah, I would try that.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for this forum. Moving discussion.

And maybe duplicating in the Linux forum. Beware of the mod there
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wrote this:



I executed this in Ubuntu, Java 8. No problems.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I just wrote this:



I executed this in Ubuntu, Java 8. No problems.


Yea but it does not work in mine. That is the problem :-) So unless we can debug the native implementation of rename in jdk source code I am going to use Files.move :-). Works every single time.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!