Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have been working on this problem before. I use a couple of different techniques. However it is difficult to have a truly cross platform solution. </p> <p>I tried doing try something like this:</p> <pre><code>String os = System.getProperty("os.name").toLowerCase(); //Windows if(os.contains("win")){ System.out.append("Windows Detected"); //set Windows Dos Terminal width 80, height 25 Process p = Runtime.getRuntime().exec("mode 80, 25"); } //Mac if(os.contains("mac")){ System.out.println("Macintosh Detected"); //... I dont know...try Google } //Linux if(os.contains("linux")){ System.out.println("Linux Detected"); </code></pre> <p>You can read/test and append "export COLUMNS" to the .bashrc file in every Linux users home directory with the String.contains("export COLUMNS") method and the user.dir property. </p> <p>That would allow you to get the columns to load up every time the java app starts up.</p> <p>Then I would pass it to a temp file. Like this:</p> <pre><code>try { ProcessBuilder pb = new ProcessBuilder("bash","-c","echo $COLUMNS &gt;/home/$USER/bin/temp.txt" ); pb.start(); }catch (Exception e) { System.out.println("exception happened - here's what I know: "); e.printStackTrace(); System.exit(-1); } } </code></pre> <p>Another option you have is to execute yor Java.jar with a bash script at startup. Inside the script you can use "tput cols" to get the width. Then pass that value to your Java app as a String[] arg. </p> <p>Like so:</p> <pre><code>//#!/bin/bash //#clear the screen clear //#get the console width and height c=$[$(tput cols)] l=$[$(tput lines)] //#pass the columns, lines and an optional third value as String[] args. java -jar ~/bin/Plus.jar $c $l $1 </code></pre> <p>why is this such a difficult task with Java? Obviously a good place to write a good API. I guess we could try Apache.commons.exec as well?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload