1

I'm trying to execute a command line to copy a folder, but nothing happens . I tried the same command from the command line and it worked fine. code

Runtime rt = Runtime.getRuntime();
    String line;
    try {

        Process pr = rt.exec("xcopy //E //I notts nots2");

        InputStreamReader mInputStreamReader = new InputStreamReader( pr.getInputStream());
        BufferedReader input = new BufferedReader( mInputStreamReader );

            while ( (line = input.readLine()) != null) 
                System.out.println(line);   
    } catch (IOException e) {
    ted=ted+1;  
    }
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • 1
    Can you tell us what gets put in the error stream? – Joe C Feb 12 '17 at 18:36
  • Also, you should be doing something with the IOException. The way the code is written is an anti-pattern. see [error hiding](https://en.wikipedia.org/wiki/Error_hiding). by the way.. who is `ted`? – vstrom coder Feb 12 '17 at 19:23

2 Answers2

1

1) pr.getInputStream() is not enough because it will not read the error output encountered during the process execution.
You should also read the error stream : pr.getErrorStream().

2) You should specify the working directory of the process otherwise the process inherits the working directory of the current process.

For example :

Process pr = rt.exec("xcopy //E //I notts nots2", null, new File("yourWorkingDirToRunTheProcess"));
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

Look at the answer to this question. It explains how to consume the standard output and standard error streams of the process.

You should also consider copying files using java API methods instead of running an external process. One reason being that your command (xcopy) won't work on anything but Windows. Another reason is that running an external process is much more error prone than using standard API methods.

Community
  • 1
  • 1
vstrom coder
  • 297
  • 1
  • 8