2

I'm using the java Process class to execute some batch files. I'm using the ProcessBuilder to create the process and this part run fine. My problem is to deal with the pausecommand in the batch file I'm running. The pause command is currently hanging the execution of the batch, but I need the batch to finish, so that I can resume pending operations. Currently, I'm starting a batch with a code equivalent to this:

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;


public class BatchEnter {

    public static void main(String[] args) throws Exception {
        List<String> params = Arrays.asList(new String[]{"cmd", "/C", "C:/test/test.bat"});
        ProcessBuilder builder = new ProcessBuilder(params);
        builder.directory(new File("C:/test")).redirectErrorStream(true);
        Process p = builder.start();

        BufferedReader wr = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while((line = wr.readLine())!=null){
            System.out.println(line);
        }

        p.waitFor();
    }
}

I'm aware that we can use this "hack" to skip the pause command from a batch file. But, my problem is I can't change any of the batch files I'm using and I can't add a batch file to call another batch like in the link provided.

I was wondering is there a way to specify to the process to skip the pause command?

Community
  • 1
  • 1
Marc-Andre
  • 912
  • 1
  • 15
  • 34
  • You could copy the batch file, remove the PAUSE and run the copy. Or you could send a keystroke to the process outputstream (not sure if it would work). – assylias Jul 17 '13 at 15:25
  • How long does your batch script take to complete the execution without the pause? – SSaikia_JtheRocker Jul 17 '13 at 15:26
  • There are different batch with different execution time. @assylias That's a good option in case the batch file is not calling anything else, but unfortunately it is not always the case. – Marc-Andre Jul 17 '13 at 15:29
  • If you know the execution time, may be you could guess the batch execution completion and use p.destroy() to kill the process. Again this not a good solution but still might work for you. – SSaikia_JtheRocker Jul 17 '13 at 15:31
  • @JtheRocker Thanks for the proposition, I will think about it. Actually running batch in our application is not something we want, but since it's a requirement we have to do some hack that I don't necessarily agree with. – Marc-Andre Jul 17 '13 at 15:34
  • Yeah, you should definitely approach the original author of the batch script and tell him to change the code to not include pause, if he wants you to use his script. – SSaikia_JtheRocker Jul 17 '13 at 15:37

1 Answers1

1

As assylias said, you just need to open an output stream and send commands to the script. That will help you with the pauses.

Sending commands to a console application?

has examples. One thing I've done before is to just read one line at a time. If the line string matches the prompt request, then send a linefeed whenever that happens. That way you don't have to worry about which scripts have prompts or how often.

Community
  • 1
  • 1
Evan Reynolds
  • 450
  • 3
  • 11
  • `If the line string matches the prompt request` what is exactly the line I should be waiting for? I've done some test with a basic batch file and there is no indication the pause command is executed – Marc-Andre Jul 17 '13 at 17:50
  • I was vague there because it just depends on what they are using to pause. On cisco there's a --More-- thing to look for, in some shell scripts I wrote there's a "Press Any Key" string, so I'd look for that. For these, is there anything that gets printed when the script pauses? Can you insert something into the batch file that gets echo'ed to the screen? – Evan Reynolds Jul 18 '13 at 21:27
  • Thanks for the suggestions but I can't rely on that since, there no standard input before the pause and I can't modify the batch but thanks it is still a valid option. – Marc-Andre Jul 19 '13 at 12:29
  • Man that's rough! The only other thing I can guess - what if you open an output stream and just send a linefeed to the process once a second or so until the process closes? I suggest that as the process will just ignore any linefeeds that aren't useful, but when it does hit a pause there'll be a linefeed there to skip past the pause. The key to THIS idea is that sending a linefeed when it isn't needed must be harmless, but from what you described I think that might work? – Evan Reynolds Jul 19 '13 at 16:41
  • I've done a little bit of testing, and sending a linefeed can bypass a pause command of a batch-file. I'll wait a bit before accepting your answer. I'll make one myself to show what I've done and my final "fix". – Marc-Andre Jul 22 '13 at 13:42