• 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

moving files using Runtime.exec() method

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 4796
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote: . . . ProcessBuilder, . . . would make the code a bit cleaner. . . . .

"Bit" is the operative word; the differences with ProcessBulider are slight.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just an FYI - exitValue codes and their meaning :


http://www.faqs.org/docs/abs/HTML/exitcodes.html
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic