Question

I am trying to execute a .bat file remotely and implementing following lines of code:

ProcessBuilder processBuilder = new ProcessBuilder(command);
    final Process process = processBuilder.start();

    InputStream stderr = process.getErrorStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;


    while ((line = br.readLine()) != null) {
        System.out.println(line);

    }
    process.waitFor();
    System.out.println("Waiting ...");

    System.out.println("Returned Value :" + process.exitValue());

but my program gets stuck inside while loop. The error it displays is:

CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

It never goes out of while loop.But it executes the script successfully. Any sort of help is appreciated. Thanks

Was it helpful?

Solution

You need to make sure you're also dealing with stderr, and you should be dealing with both streams in separate threads.

Read this and make sure you follow all the advice.

Edit: Looking at the code you've written, you seem to have reproduced code from this precise article. In fact, it looks like listing 4.3 (MediocreExecJavac.java).

OTHER TIPS

Add redirectErrorStream(true) before you .start() and read getInputStream(). This will read stderr and merge that with stdout into one response which can be read with getInputStream().

    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectErrorStream(true);
    final Process process = processBuilder.start();

    InputStream stderr = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;


    while ((line = br.readLine()) != null) {
        System.out.println(line);

    }
    process.waitFor();
    System.out.println("Waiting ...");

    System.out.println("Returned Value :" + process.exitValue());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top