Home » Console: Java Console Applications

Share This Post

Console Applications

Console: Java Console Applications

Console: Java Console Applications

Java Console Applications

The console is, of course, the command prompt. On Windows go to [Start][enter ‘cmd’ into search] to bring up a command prompt. Could be called a shell also or power shell. Its the text-based screen window for entering commands. 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 for the console

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 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.

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 are 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.

Standard Input, Output and Error

Console Input

import java.util.*;
public class Input{
 public Input(){
  String input="";
  Scanner scan= new Scanner(System.in);
   input= scan.nextLine();
   System.out.println("You Entered:"+input); // echo input to the console
  } // will get input until the command of quit is entered
 public static void main(String args[]){
  new Input();


Java for a long time didn’t support input from console well. They finally added the Scanner class which handles it well.
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 basically does 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 pop up 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.


Share This Post

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>