1

I am running a Shell script using cygwin.

Process p;
InputStream in;
BufferedReader br;
String line;
String cmd;
cmd = "D:/cygwin/bin/bash -c '/bin/test/app.sh" +three_ltr_id+""+mon_code+""+year_code+""+part_no+""+version_no+" '";
System.out.println("EXECUTING: " + cmd);
p = Runtime.getRuntime().exec(cmd);
in = p.getInputStream();
p.waitFor();
 br = new BufferedReader(new InputStreamReader(in));
 System.out.println("OUT:");
 while ((line = br.readLine()) != null) {
 System.out.println(line);
 System.out.println("SCRIPT EXECUTED PROPERLY");

This is showing EXECUTING and the commands that I passed to script.

If I go inside D:/cygwin/bin/test folder and run the same command it works.

When I run the same command at the command line it won't work.

2
  • 1
    What do you mean by "it wont work"? What error you are getting? The command is not found? Or the command is found but the output is not okay? Commented Dec 27, 2011 at 15:28
  • Output is not correct like /bin/test/app.sh: line 226: get_ProgramID: command not found Commented Dec 27, 2011 at 15:34

1 Answer 1

2

You need to start reading the input from p.getInputStream() immediately, and keep reading it until there is no more. On Windows, there is little or no buffer in the pipe, and the process will hang once it is filled.

Same is true for the error stream. You could launch threads to read both streams, or there's an option in the way you launch processes to combine regular output and errors, and you can just read them from there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.