1

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
barney
  • 11
  • 1
  • 2
  • 5
    http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java - no need for exec. – Erik Apr 07 '11 at 21:06

3 Answers3

4

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.)

Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
0

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

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.

user unknown
  • 35,537
  • 11
  • 75
  • 121