aspose file tools*
The moose likes Java in General and the fly likes moving files using Runtime.exec() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "moving files using Runtime.exec() method" Watch "moving files using Runtime.exec() method" New topic
Author

moving files using Runtime.exec() method

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
hi all,
i have a simle java method where i am trying to move files from one directory to another. Below is the code





there are no exceptions thrown in java but for some reason it sometimes give an exit value which indicates an abnormal termination, i.e exit value of 2. I cant figure out why.

any suggestions would be greatly appreciated.

thanks.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
I don't know what a return code of 2 indicates.

However you should not be using the "mv" command since this is a platform specific solution.

Just use the File.renameTo(..) method.
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
thanks for the reply. this code will always be run on a solaris platform so am not worried about the code being platform specific at the moment.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Even so, why not try using renameTo()? I suspect it will be considerably easier than what you're doing, and just as fast.

However if for some reason you really need to do this with Runtime.exec(), I recommend reading When Runtime.exec() won't. The article is over ten years old but still the best source I know of for this information; the only thing really missing is that it hasn't been updated to include ProcessBuilder, which would make the code a bit cleaner. However most of the key issues remain the same with ProcessBuilder as with Runtime.exec(). I think it's likely that the most useful advice for you will be to look at the error stream and see what it says. There's probably a message there that will give a clue about what the problem is.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Mike Simmons wrote: . . . ProcessBuilder, . . . would make the code a bit cleaner. . . . .
"Bit" is the operative word; the differences with ProcessBulider are slight.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Mike Simmons wrote:Even so, why not try using renameTo()? I suspect it will be considerably easier than what you're doing, and just as fast.

And also probably has the advantage that if there is something preventing the move, such as access permissions, you're more likely to get a useful error message.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19542
    
  16

Depends on what you find useful. File.renameTo returns false if it fails, but it never tells you why it fails. That has been solved in Java 7 with the java.nio.file.Path class and its moveTo method. It throws an exception instead when it fails.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Yeah, the lack of useful error messages is the one big problem I have with File.renameTo(). Still, it's relatively easy to try it and see if it works.

One nice thing about ProcessBuilder is the ability to redirect the error stream to regular output. Doing this removes the need to process the two streams in separate threads, which is considerably simpler. This can be done with Runtime.exec() as well, by passing a redirect like "2>&1" to the operating system. But that's OS-dependent, and less understandable to most readers.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Rob Spoor wrote:Depends on what you find useful. File.renameTo returns false if it fails, but it never tells you why it fails. That has been solved in Java 7 with the java.nio.file.Path class and its moveTo method. It throws an exception instead when it fails.

Ah, yes, I'd forgotten that. Just assumed it threw an exception without checking. Ignore what I said, then .
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Until JDK 7 is out, I'd probably use Google Guava's Files.move() method. Or there's a similar one in Apache Commons somewhere.
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
just an FYI - exitValue codes and their meaning :


http://www.faqs.org/docs/abs/HTML/exitcodes.html
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 556
    
    7

from your post

Advanced Bash-Scripting Guide

you are running java -- are you guranteed to have the same results?

Are you guranteed to read the same results (a much more important question)?

If so..... please post your sources to the yeses above.

-steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: moving files using Runtime.exec() method
 
Similar Threads
System.exit(1) is called inspite of it not being specified in the code
calling Unix script from Java
About catching Exception instead of more specific exception.
XML Schema Validation with Xerces
How to handle/kill a hung thread