Java - Command Line Programs
Java - Command Line Programs
Written by Ian Elliot   
Wednesday, 30 May 2012
Article Index
Java - Command Line Programs
Data Types
Try It Out - Java Guess

Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

In previous chapters of our introduction to Modern Java we have looked at how to make use of the Swing library to create simple programs. Sometimes it is worth taking a step back and doing things in the simplest possible way. In this chapter we take a careful look at how code and data types build a program.

Modern Java
With NetBeans And Swing



  1. Getting started with Java

    In chapter 1 we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 

  9. Java Data Types - Numeric Data
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 



Java programs can take a number of different forms from complex user interfaces based on Swing or the latest JavaFX, or they can run within a browser. The simplest of all Java programs just makes use of the command line to interact with the user via text. The program can display text and can accept text typed on the keyboard. Sometimes is is enough!

To see how this works start NetBeans running and use the command File, New Project and select Java Application from the Java folder. Call the project HelloCL and make sure that the Create Main Class option is ticked.

When the project has been created you should see that within the hellocl package there is a class containing the code (with some irrelevant lines removed):


package hellocl;public class HelloCL {
 public static void main(String[] args) {
  // TODO code application logic here


This is the simplest Java program you can create. It takes the form of a single class HelloCL. At this stage just think of a class as a useful way to group code together. Within the class is a single method called main - this is where the program starts running. 

All of this code has been generated by NetBeans for you but you could have typed it all in for yourself.

At the moment the generated program doesn't do any thing so let's, in the grand tradition of programming, add a line that prints "Hello":

 public class HelloCL {
 public static void main(String[] args)

The only thing that is new here is the use of the System.out object - a standard object provided by the Java Framework which has the println method, which will print any text you specify to the console.

If you run the program you might be puzzled as to where the output appears because at first sight nothing much seems to happen. However, if you look at the window in the bottom right of NetBeams - labeled Output - you will notice that it says Hello -




This is a very simple program and you might be wondering if you really need the whole of NetBeans and the Output window it provides to run it - you don't.

When NetBeans runs a program it compiles it to an intermediate code called byte code which the Java interpreter or Virtual Machine (VM) runs. You can just as easily feed the intermediate code to the VM yourself outside of NetBeans.

Exactly how to do this depends on what operating system you are running and so on. It can be tricky to get right. You can get NetBeans to tell you where the compiled code is and how to run it.

Use the command Run,Clean and Build Main Project. This deletes all of the files that have accumulated and creates a new class file ready to run. The progress of the build is reported in the Output window and close to the end you should see the instruction need to run the program. It should be something like:

To run this application from the command line without Ant, try:
java -jar "C:\NetBeansProjects\HelloCL\dist\HelloCL.jar"

In case you are wondering, a .jar file is a collection of files needed to run a program all zipped together. If you type the command in at the command prompt on whatever operating system you are using then you should see the "Hello" output.

This works for any type of Java program, not just ones that use only the command line.

One thing you need to remember is that:

Java source code is case sensitive and this means the identifier Total is different from the identifier total.

Last Updated ( Thursday, 09 June 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2016 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.