tags:

views:

628

answers:

7

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();
+2  A: 

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.

Ben S
If this were some *nix flavor, I'd agree, but in practice it is really hard to address a lot of Windows network resources in Java and it's more robust just to execute copies and deletes in the shell.
Cameron Pope
The linked example is a terrible reference for a binary file copy; treats the file as characters (what happens if there is an odd number of bytes) and is horribly inefficient.
Software Monkey
+4  A: 

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.

Cheers, V.

vladr
What was the difference between /C and /K again?
OscarRyz
@Oscar - /C will close the window after execution, /K will keep it open.
John T
A: 

Check this out, this may help you:

http://www.greenspun.com/bboard/q-and-a-fetch-msg.tcl?msg_id=006Tv9

+2  A: 

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"

Peter Richards
You can always vote up my answer :D
Ben S
Actually, with my mighty 1 reputation, I cannot.
Peter Richards
Whoops, good point. Well in that case, welcome to SO!
Ben S
Modified your answer because you forgot to double-quote your double-backslash example :) Apparently it's needed here as well as in your Java code (requiring an overall 4 backslashes for this example to represent a single backslash!). Anyway, +1 4u
Bill K
Ha! Well that probably explains Zombie8's post lacking them then. Thanks.
Peter Richards
+1  A: 

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.

Cameron Pope
A: 

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)

Joshua
A: 

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

Bill K