Issue
I am working on a project in Java where I am trying to get the size of the terminal window. I have found that stty size
returns the size in rows and columns which I am fine with.
My problem is to get the values using Java. So far I have this:
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("stty", "size");
try {
Process process = processBuilder.start();
InputStreamReader is = new InputStreamReader(process.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(is);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
This outputs:
[I@6ce253f1
However, when I just run stty size
on my own, I get 30 213
. I assume it is some sort of encoding issue but I am not sure.
Solution
Try
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("stty", "size");
processBuilder.redirectErrorStream(true);
processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
try {
...
Then you get an error message which is written to the error channel, too. And the input channel is set to the same input channel the Java process is using.
Answered By - vanje