barney
barney

Reputation: 11

Runtime.getRuntime.exec() problem with copying files

I want to copy a file using this :

Process process = Runtime.getRuntime()
     .exec("cmd.exe /c  copy  C:\test1\toto.PDF  C:\test2\toto.PDF");

When i execute the command manually, it works, but when i tried to do it from my IDE, nothing happened. can someone tell me what is wrong with this please.

thanks.

Upvotes: 1

Views: 2966

Answers (3)

user unknown
user unknown

Reputation: 36250

Here is a longer article about pitfalls with runtime.exec Is copy a built-in of cmd.exe, or a separat executable?

I would cut the string into parts, to avoid misinterpretation of blanks/tabs:

"cmd.exe", "/c", "copy", "C:\test1\toto.PDF", "C:\test2\toto.PDF"

But this is all very platform dependend. You should read the file with java and write it to the target location.

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86774

Your immediate error is that \t is a tab character. You forgot to double the backslashes, so the file names got mangled. However, as others have suggested, use Commons IO-Utils to do the copy.

Upvotes: 0

Simon Nickerson
Simon Nickerson

Reputation: 43157

You will need to double the backslashes; \t by itself translates to a tab character.

(You were rather unlucky here, as if your path had been different, you might have got a compiler error that gave you a hint.)

Upvotes: 4

Related Questions