Reputation: 141
I am looking to open up a command prompt and pass in a copy command, some switches, and the source file plus destination. I've tried the code below but nothing appears to be happening. What am I not seeing? What could I be doing wrong?
String line;
line = "cmd COPY /Y C:\srcfolder\112.bin C:\destfolder";
Process p = Runtime.getRuntime().exec(line);
p.waitFor();
Upvotes: 2
Views: 11738
Reputation: 24770
For those refering to Apache Commons IO.
Since java 7 there is also a java.nio.Files#copy(Path, Path, CopyOption)
method, which is very convenient. You can find more information here.
Since java 7, the use of Path
objects is also prefered in favor of the previous File
objects.
Upvotes: 1
Reputation: 66721
If you really have to use an external command, then you probably want to execute (notice the /C):
CMD /C COPY /Y C:\srcfolder\112.bin C:\destfolder
I recommend you use the array version of exec
to avoid handling of quoting (should any files or directories contain spaces - or double-quotes - in them):
String[] args = { "CMD", "/C", "COPY", "/Y", src_file, dest_folder };
Process p = Runtime.getRuntime().exec(args);
p.waitFor();
Remember that this is not portable (will not work on Unix), so unless you really really need to use COPY
then you should use the method linked to by bstpierre
.
Upvotes: 10
Reputation: 629
I second bstpierre's comment.
In reference to your particular problem, I believe that the cmd shell is not exiting after you create it. (edit: and Vlad has pointed out how to correct that)
As an aside, for other commands in the future, don't forget to escape your backslashes: line="cmd copy /y c:\\srcfolder\\112.bin c:\\destfolder"
Upvotes: 2
Reputation: 62789
Ahh, looks like someone did mention it, but I'll clarify (epically because the one that did mention it forgot to quote their backslash in the post, making it look like a single!).
So the solutions listed are better, but but I'm fairly sure that the reason you are failing is that in Java you can never use back slashes as singles, they are the quote character so you always need \\ inside a string. And for 2 backslashes in a row, I think you need 6 or 8 of them !!?!?? look it up.
Fixed the guy who posted it before me and gave him a +1
Upvotes: 0
Reputation: 26742
Use the to use the windows version.
CMD /C COPY /Y C:\srcfolder\112.bin C:\destfolder
An alternative: Apache Commons IO provides a nice set of libraries to handle file transfers with pure Java. Specifically look at FileUtils.copyFileToDirectory(File srcFile, File destDir)
Upvotes: 0
Reputation: 7745
try
line = "cmd /C COPY /Y C:\srcfolder\112.bin C:\destfolder";
Process p = Runtime.getRuntime().exec(line);
p.waitFor();
However, you'll run into problems if you have files and folders with spaces in them. I've found the most robust way to execute commands is to use ProcessBuilder, and pass in the command with all of the arguments as parameters.
Upvotes: 1
Reputation: 69382
Is there a reason you aren't simply copying the file in Java rather than creating a system process?
Copying the files using Java rather than an exec call would keep your code portable.
Upvotes: 2