Skip to content

Instantly share code, notes, and snippets.

@kalloc
Last active May 8, 2021 23:05
Show Gist options
  • Save kalloc/307ae7c9ce6d2924e895d393833c7c5e to your computer and use it in GitHub Desktop.
Save kalloc/307ae7c9ce6d2924e895d393833c7c5e to your computer and use it in GitHub Desktop.
import java.io.*;
import java.lang.ProcessBuilder;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
// create a new list of arguments for our process
String[] list = {"bash", "-c", "yes Syntax Warning: Length in linearization table is invalid | head -n 10000 >&2"};
// create the process builder
ProcessBuilder processBuilder = new ProcessBuilder(list);
processBuilder.redirectErrorStream(false);
InputStream inputStream = null;
String line = null;
try {
Process p = processBuilder.start();
inputStream = new BufferedInputStream(p.getInputStream());
// Reads first byte from file
int i = inputStream.read();
while (i != -1) {
System.out.print((char) i);
i = inputStream.read();
}
if (!p.waitFor(5, TimeUnit.SECONDS)) {
p.destroy();
p.waitFor();
}
inputStream.close();
} catch (Exception e) {
System.out.println("Some exception");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment