I am trying to execute commands using Java. So when I try to convert the process Inputstream to string using BufferedReader, the code works if I call process.waitFor() after processing the inputstream to string. But when I try to convert the process input stream using ByteArrayOutputStream to string, the results are not returned if I write process.waitFor() after processing the inputstream to string. It works only process.waitFor is written before inputstream.isavailable(). I don't understand why this is behaving like this? Also I want to know how to determing the buffer array size incase of using ByteArrayStream. I am trying to use isavailable() to know to number of bytes.
``ProcessBuilder pb = new ProcessBuilder();
String cmd = "ls -l /Users/uma/data";
pb.command("bash", "-c",cmd);
try {
Process process = pb.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
intexitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
try (final BufferedReader b = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String errorline;
if ((errorline = b.readLine()) != null)
System.out.println(errorline);
} catch (final IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
`
``ProcessBuilder pb = new ProcessBuilder();
String cmd = "ls -l /Users/uma/data";
pb.command("bash", "-c",cmd);
try {
Process process = pb.start();
int exitVal = process.waitFor();
InputStream is = process.getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = newbyte[is.available()];
int length;
while ((length = is.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
String output = result.toString();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
try (final BufferedReader b = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String errorline;
if ((errorline = b.readLine()) != null)
System.out.println(errorline);
} catch (final IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}`
How to determine the buffer size? Also when should I call waitFor()?
waitFor()
waits for the target process to exit, and returns the exit code given by it. You can use it to wait for the program to stop, before logging any errors or checking the exit code.As for the buffer size, I recommend using
is.readAllBytes()
if you just want to get the entire input stream as a byte array. To then convert that to a string, usenew String(is.readAllBytes());
.If you just want to stream the content of the input stream into another stream (such as System.out or System.err), use this:
This will just write the entire
is
input stream into System.out, you can configure or change this as you'd like.