How to Find the Console Width with Java

Can I find the console width with Java?

There are no reliable cross-platform solutions to this problem. Indeed, there are situations where it is not possible to know what the real console width is.

(See other answers for approaches that work some of the time and/or on some platforms. But beware of the limitations ...)

For example, on a Linux system you can typically find out the notional terminal dimensions from the LINES and COLUMNS environment variables. While these variables are automatically updated when you resize some "terminal emulator" windows, this is not always the case. Indeed, in the case of a remote console connected via telnet protocol, there is no way to get the actual terminal dimensions to the user's shell.

EDIT: Just to add that if the user changes the dimensions of his/her xterm on Linux after launching a Java app, the Java app won't be notified, and it won't see the new dimensions reflected in its copy of the LINES and COLUMNS environment variables!

EDIT 2: My mistake: LINES and COLUMNS are bash shell variables, and they are not exported to the environment by default. You can "fix" this by running export COLUMNS LINES before you run your Java application.

Java: Getting command-line size in characters

It seems you need a way to get a console information (its width, height). You can find more information in this question: Can I find the console width with Java?.

If you are not looking for a cross-platform solution, that works with all kinds of terminals, you may find JLine 2 useful.

Is there a way for Java to determine the size/dimensions of the command-line console it's running in?

You would need to use JNI to call ncurses or use a library that does the same.

If all you need is to get dimension information, there's an example here that has API calls to getRowCount and getColumnCount.

How do I find the width & height of a terminal window?

  • tput cols tells you the number of columns.
  • tput lines tells you the number of rows.

java processbuilder terminal width

The terminal width is determined by the terminal program your using. Each terminal program has you set the width a different way (usually in the settings somewhere). If they respond to any sort of global variable or command sequence then it may be different for each terminal program (and some dont have this feature). However there is a standard solution that some terminal programs have adopted.

Only way would be to find a terminal program that does respond to a specific global variable or command sequence and force your user to invoke the application using that particularly terminal program.

The closest solution there is to a standard would be the following control sequence which will resize certain terminal emulators:

System.out.println("\e[8;50;100t")

The above will resize your terminal to 100x50

However the above will not work with all terminal emulators there are several that will recognize it. Just need to ensure the user is using a terminal program that supports the standard.

See XTerm Control Sequences reference for more information.

Change console size in Eclipse

As answered in comments, the Console view will take the entire width if the Project Explorer view, which is currently on its left, is moved to the left of the text editor. This way, the Project Explorer will be above the Console as shown in the screenshot below.

Screenshot of Eclipse IDE with the Console view taking all the width

Note: a view can be moved by drag-and-dropping its title bar.



Related Topics



Leave a reply



Submit