Java - Command Line Programs
Written by Ian Elliot   
Friday, 01 July 2016
Article Index
Java - Command Line Programs
Data Types
Operators And An Example
The Complete Program

Many Java programs don't make use of a GUI interface at all they are run from the command line. It is important to master this simpler form of program if you are going to build utilities implement services or more trendy microservices. From the point of view of learning Java command line programs are also a good way to find out about some basics without the complexity of Swing to worry about. 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 look at command line programs that have no GUI. 

 

Modern Java
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events

 

 

Java programs can take a number of different forms from complex user interfaces based on Swing or the JavaFX.

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!

Hello Command Line World

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.

hello
When the project has been created you should see that within the hellocl package there is a HelloCL.java 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)
 {
  System.out.println("Hello");
 }
}

 

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 i.e print - line, which will print any text you specify to the console on a single line.

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 -

 

hellorun

 

Running Without NetBeans

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.

There is a much more basic way to create and run Java programs. You don't need to know how all this works at this stage and so feel free to skip this section. However if you spend a few minutes trying this out you will understand more about Java and the way it works.

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 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, or better copy and paste it, 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.

Knowing how to run a Java program from the command prompt frees you from NetBeans and it is how you would run your program when its development phase is over and you just want to use it.



Last Updated ( Friday, 01 July 2016 )