Technology Programming

How to Write Java Source Code

Introduction

Note: Before you start this tutorial, you must have downloaded and installed the Java SE Development Kit.

It’s traditional to start learning a new programming language by writing a program called "Hello World". You can think of it as a very simple initiation into the ranks of Java programmers. All the program does is write the text "Hello World!" to your computer screen.

The basic steps we will follow for our Hello World program are:
  1. Write the program in Java
  2. Compile the source code
  3. Run the program


Write the Java Source Code

The first thing you need is a place to write your Java code. All Java programs are written using plain text files, therefore you can write them without any fancy software. For your first program, open up the simplest text editor you have on your computer. I’ll be using Notepad.

The program code looks like this:

// The classic Hello World program! class HelloWorld {   public static void main(String[] args) {      //Write Hello World to the terminal window       System.out.println("Hello World!");   } }
You could cut and paste the above code into your text editor, but it’s better to get into the habit of typing it in. It will help you learn Java quicker because you will get a feel for how programs are written, and you will make mistakes! It may sound odd, but any mistakes you make will help you become a better programmer in the long run. Just remember that your program code must match the example code, and you’ll be fine.

For this first program, I’m not going to explain how it works or what all the code means; that will come in later tutorials. At the moment, it’s more important for you to experience the steps you should follow to create and run a program.

Save the File

Save your program file as "HelloWorld.java". I’ve created a directory in My Documents called "Java" to store all the example Java programs in one place. It makes it easy for me to find them when I need them. You might consider creating a similar directory on your computer.

It’s very important that you save the text file as "HelloWorld.java". Java is a little bit picky about filenames. If you look at the code you will see the statement:

class HelloWorld{
It’s an instruction to call the class "HelloWorld". The filename must match this class name, hence the name "HelloWorld.java". The extension of ".java" tells the computer that it’s a Java code file.

Open a Terminal Window

Most programs that you run on your computer are windowed applications; they work inside a window that you can move around on your desktop. The HelloWorld program is an example of a console program. It does not run in its own window; it has to be run through a terminal window instead. A terminal window is just another way of running programs stored on your hard drive.

To open a terminal window, press the "Windows key" and the letter “R”.

You will see the "Run Dialog Box". Type "cmd", and press "OK".

A terminal window will appear on your screen. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files they contain, and run programs. This is all done by typing commands into the window.

The Java Compiler

Another example of a console program is the Java compiler called "javac". This is the program that will read the code in the HelloWorld.java file, and translate it into a language your computer can understand. This process is called compiling. Every Java program you write will have to be compiled before it can be run.

To run javac from the terminal window, you first need to tell your computer where it is.

On my machine it’s in a directory called "C:\Program Files\Java\jdk\1.6.0_06\bin". If you don’t have this directory, then do a file search in Windows Explorer for "javac" to find out where it lives.

Once you’ve found its location, type the following command into the terminal window:

set path= *the directory where javac lives*
E.g.,

set path=C:\Program Files\Java\jdk\1.6.0_06\bin
Press Enter. The terminal window won’t do anything flashy, in fact it will just return to the command prompt. However, the path to the compiler has now been set.

Change the Directory

Next, navigate to where your HelloWorld.java file is saved. As I said earlier, my file has been saved in a directory called "Java" in My Documents. Its location is "C:\Documents and Settings\Paul\My Documents\Java".

To change the directory in the terminal window, type in the command:

cd *directory where HelloWorld.java file is saved*
E.g.,

cd C:\Documents and Settings\Paul\My Documents\Java
You can tell if you’re at the right directory by looking to the left of the cursor.

Compile Your Program

We’re now ready to compile the program. To do so, enter the command:

javac HelloWorld.java
After you hit Enter, the compiler will look at the code contained within the HelloWorld.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.

Hopefully, you should get the same result as me and have no errors. If that’s not the case, go back and check the code you’ve written.

Make sure it matches the example code and re-save the file. Keep doing this until you can run javac without getting any errors.

Tip: Once your HelloWorld program has been successfully compiled, you will see a new file in the same directory. It will be called “HelloWorld.class”. This is the compiled version of your program.

All that’s left to do is run the program. In the terminal window type the command:

java HelloWorld
When you hit Enter, the program runs and you will see "Hello World!" written to the terminal window.

Well done. You’ve written your very first Java program!

I’ll admit it’s not the most exciting program ever written, but you now know the steps every Java program has to go through:
  1. Write the Java code in a text file
  2. Save the file
  3. Compile the source code
  4. Fix any errors
  5. Run the program

As the programs get more interesting, these steps will become second nature.

Leave a reply