I'm currently trying to get the number of columns the terminal has in Java. For that I know there is an environment variable $COLUMNS, so I try to run 'echo $COLUMNS' and read its output, but for some weird reason I get a value that makes no sense, and on top of that, I always get the same value, no matter how I resize the terminal.
This is my code:
public static final String[] COLS = {"/bin/sh", "-c", "echo $COLUMNS </dev/tty"};
public int getColumns(){
try{
Process p = Runtime.getRuntime().exec(CONST.COLS);
p.waitFor();
InputStreamReader in = new InputStreamReader(p.getInputStream());
int cols = in.read();
return cols;
}catch(Exception e){
System.out.println(e);
}
return 0;
}