Page 1 of 4 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
Contents
- Why Java?
- Getting started with Java
- Introducing Java - Swing Objects
- Writing Code
- Command Line Programs
- User Interface - More Swing
- Working With Class
- Java Class Inheritance
- Java Data Types - Numeric Data
- Java Data Types - Arrays And Strings
- Building a Java GUI - Containers
- Advanced OOP - Type, Casting, Packages
- Value And Reference
- 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 with Maven, Java Application from the Java folder. Call the project HelloCL and accept all of the defaults. This creates an empty project for you to us:
If you right click on the package and select New,Java Main Class. You may have to select Other to see the full list of options. Call the class HelloCL
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):
public class HelloCL {
/** * @param args the command line arguments */ 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.
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 now run the program you will see a dialog box appear which asks you to select the main class i.e. the class that has the main function you want to run. In this case there is only one class so select it.
The program is compiled and after a while it will start. 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 -
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.
To run the application you have just got NetBeans to compile is fairly easy. First you have to find the class file that the compiler has created. The directory you need is:
C:\Users\username\Documents\NetBeansProjects\HelloCL\target\classes
The simplest thing to do is to make this your current directory. Then use the command:
java com.mycompany.hellocl.HelloCL
You will see hello printed in the command window. In general you have to refer to the class as
packagename.classname
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.
|