• Post Reply Bookmark Topic Watch Topic
  • New Topic

run native move command in Java  RSS feed

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have to zip all the files in a folder which has the timestamp more than 90days, for this i want to move all the files to a filder which ever has > 90days timestamp, to achive this i have used IO package, but the original time stamps of the files are changing to current date, to avoid this i started writing code in native "move" command, like below:



I am getting the below error, please help:

java.io.IOException: Cannot run program "move": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.ssga.jar.job.Sample.main(Sample.java:25)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
 
Saloon Keeper
Posts: 4038
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be other problems, but for starters, use Runtime.exec(String[]), not Runtime.exec(String).
 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried with String array too.
String st[] = {"move","sourcefodler","destinationfolder"};
Runtime.getRuntime().exec(st);

This also gives the same error :-(
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "move" command is part of the cmd.exe Windows shell interpreter. It is not a standalone application. You'd therefore need to execute the following command line:

cmd /c move file1 file2

Also note that your files contain spaces and you'd need to enclose them in double quotes. Furthermore, you need to read this famous article before you write code to execute an external program.

All in all, this is a lot of work to do an operation which is naturally supported by Java (see File.renameTo()). Of course, if you're moving across drives, that would be different, but there are still libraries like Apache Commons which I'd prefer to do this kind of job.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem might be the spaces in "Documents and Settings". Try putting quotes around the file paths. And change your variable names while you're at it to make your code cleaner: para and para1 are not very descriptive


Better:

Or:


Pick your poison

Edit: Of course, there's all that Martin said above (posted while I was crafting this reply )
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!