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 Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

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 ( Saturday, 21 May 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.