If you want to write an interactive terminal / console based application in Java (perhaps on a headless server) then Lanterna (a Google Code project) makes this very easy.
Download the Lanterna jar from http://code.google.com/p/lanterna/ and here is a simple example to get you started:
import com.googlecode.lanterna.*;
import com.googlecode.lanterna.terminal.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Term
{
private Terminal term;
private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public Term()
{
term = TerminalFacade.createTerminal();
term.enterPrivateMode();
while (true)
{
term.clearScreen();
String dateString = df.format(new Date());
term.applySGR(Terminal.SGR.ENTER_BOLD);
term.applyForegroundColor(Terminal.Color.RED);
show(dateString, 0, 0);
term.flush();
try
{
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
private void show(String str, int x, int y)
{
term.moveCursor(x, y);
int len = str.length();
for (int i = 0; i < len; i++)
{
term.putCharacter(str.charAt(i));
}
}
public static void main(String[] args)
{
new Term();
}
}
import com.googlecode.lanterna.terminal.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Term
{
private Terminal term;
private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public Term()
{
term = TerminalFacade.createTerminal();
term.enterPrivateMode();
while (true)
{
term.clearScreen();
String dateString = df.format(new Date());
term.applySGR(Terminal.SGR.ENTER_BOLD);
term.applyForegroundColor(Terminal.Color.RED);
show(dateString, 0, 0);
term.flush();
try
{
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
private void show(String str, int x, int y)
{
term.moveCursor(x, y);
int len = str.length();
for (int i = 0; i < len; i++)
{
term.putCharacter(str.charAt(i));
}
}
public static void main(String[] args)
{
new Term();
}
}
Build on the command line using:
javac -cp lanterna-2.0.4.jar Term.java
Run on the command line using:
java -cp lanterna-2.0.4.jar Term