Java Console Applications

Written by Larry Gray on . Posted in Console Applications

Java Console Applications

I gave a very small introduction to Java console application execution in the Java Desktop applications article. Java console applications are started in the same manner. I talked about path and classpath which are both very important. If the path and classpath are not set properly then your console application will fail to execute. I explain how to set the classpath on windows in the Desktop Applications article. There is much help on the internet for telling you how to set the system path to add java bin folder. If you do not add the JDK bin folder to the path then when you type 'java' at the command line or any other java tools you will get a not found error.

(DOS OR Windows Shell example)

set path=%path%;c:\dev\java\jdk8\bin;
set classpath=%classpath%;.;c:\dev\java\myproject\bin;
echo %path%    shows you the current path setting
echo %classpath%   shows you the current classpath setting

Java was not designed from the ground up for operation at the console(text screen) as a user interface. The standard Java API's and installation provide only very basic access to the system console. Java was made more for the windows GUI and is used more as background processes like servers. Many server applications are started from the console or from scripts which are run by the system. Some popular java utility applications operate from the command line. For more advanced console programming 3rd party API's are needed such as JCurses. JCurses might allow more complete control of the screen as in plotting characters to exact x,y positions. JCurses may let you alter background colors and foreground colors. Another 3rd party API is Java Native Access which I have not tried. This lib will allow you to output text in color to the console from what I understand. Another option for someone wanting to step back in time and have a terminal would be to make your own GUI component which emulates a text screen(JPanel), system input(KeyListener) and system output (plotting ASCII characters(drawImage()) in cells).

Below is an example of where I used JCurses with my Java Ledger project.

This slideshow requires JavaScript.

Java console or GUI applications may be given arguments when started from the command line in the console. This is merely a list of words which were separated by spaces in the command. i.e. 'java MyJavaApp Hello World' might return Hello (new line) then World (new line). Most command line commands have options. With Java arguments given to the main method (which is the starting point in any java application) the programmer may code options into the startup of the application using whatever syntax he likes such as '/s /q' or '-sq' or whatever seems appropriate.

Using the Java System object one may access the 'standard input', 'standard output' and ' standard error'. Standard input will allow the programmer to gather a line of text at the end(bottom) of the console from the user. System output allows the java programmer to output lines of text at will to the console. System execute command would allow the programmer to use a DOS command like CLS to clear the console. This isn't robust but it works and can get the job done. Also Java apps can handle redirection of input output in the same manner as any other system processes. I'm fairly sure you could direct the output of one Java application as input for another for example. You may certainly redirect the output from a Java application to a text file.

Pieces of example code.

// here we make a BufferedReader to get a line of text from the console.
br = new BufferedReader(new InputStreamReader(; // here we use
//next in an infinite loop use
String input = br.readLine();
// then you can output the result back to the console
System.out.println("input:"+input);  // here we see the use of System.out
// if you throw exceptions in your code the output will go to System.err (error).

To be honest I have not seen many or any Java console apps that request key input from the console. I use the console mostly for quick and dirty debugging output. Though logging API's basically do the same thing better but with files or console. If a person wanted too, he could grab some input by using JOptionPane.showInputDialog() method. Using this in a console app means a GUI dialog will popup in front of the console and request input in a text field. When the user hits enter the dialog closes and text is returned as a String from the method call. You may do this without even using or displaying a JFrame.

In conclusion the console even with Java is useful and necessary at least for execution of applications and output from applications. I am even working on a text screen emulation component for the GUI as I described above for a retro text based adventure game using Java.