Writing your first program

The traditional way to start learning a programming language is by creating a small program that simply sends the string "Hello World" to the console (the command line, or DOS window in Windows 98/ME/2000 or XP. I have created an animated capture of the proces of creating this program as the next resource in this tutorial. I will be assuming that you are using the naked JDK with a simple editor such as notepad.exe, that comes with Windows. If notepad is a little unexceptional you might like to consider notepad2 from www.flos-freeware.ch which is a “drop in replacement” for windows notepad but also includes some programmer specific features such as color syntax highlighting. The editor has a built in knowledge of the syntax of Java and colors the keywords accordingly. Whatever editor you use it must be able to save files with the extension .java.

If you are running a version of Windows and are not familiar with the console, chose the start menu, chose run and if you are on Windows 2000 or XP type cmd, if you are on Windows 95/98/ME type in "command". You will then see the black window of mystery that is the command prompt, or console. If you type in notepad you will see a real windows program start which is the minimal editor that ships with just about all versions of windows.

Create a file called HelloWorld.java. and enter the following code.

 
public class HelloWorld{
public static void main(String argv[]){
        System.out.println("Hello World");
        }
}

Now compile the code by typing

javac HelloWorld.java

If there are no problems you should return to the prompt without seeing any confirmation message. You may get an error message saying something like

public class Helloworld must be defined in a file called "Helloworld.java"

This indicates that the name of the class in the file does not exactly match the name of the file containing the code. This is because Java checks that the case of the letters in the file name matches the case of the letters in the class name. This can seem a little odd if (like me) you are used to the DOS/Windows platform where the case of a file name has almost no significance. Checking the case of letters is the first thing to examine when you have an error, particularly when you are first starting to work with Java. Make sure you have the semi colon at the end of the System.out... line.

If you get an error indicating that the system knows nothing about Javac then you may have to investigate the path setting in your system. This should be set to include the location of the Java binaries (the programs that actually make it go). On a Windows system these files are typically kept in a directory called something like

 c:\jdk1.5\bin

If you have managed to compile up the program without error, do the DOS dir command to see what files have been produced.

You should see a new file has been created called

HelloWorld.class

The class file is what will actually be run. To run it you type

java HelloWorld

More information on creating your first program can be found at http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

Java is thoroughly case sensitive

This program should now output the string "HelloWorld" to the console. Note that you only typed the name of the class file and did not give the .class extension, even though you had to give the .java extension when you compiled the program. If the code in your class says

public class HelloWorld 

But you typed

java Helloworld

On the command line, you will get an error. I can't emphasise enough that java is thoroughly case sensitive.

The error output under JDK 1.4

Last modified: Thursday, 24 July 2014, 2:54 PM