Java Programming

Introduction > Running your first application with the JDK

To test if the Java Runtime Environment, the part that runs Java applications, is correctly installed, simply type at the command prompt:

java -version

and press enter. The result of this operation should display the version of Java you are currently running. With the JDK 1.3, the result should look similar to this:

Command Prompt

If the operation fails, you may need to restart your computer, or attempt re-installing the JDK. What we are actually doing when we type "java" at the command prompt, is executing an application called "java.exe" which is either located in the windows directory (usually c:\windows) or, if you are using a recent version of windows, in the windows system directory (usually c:\windows\system32). What you are doing hence when your run an application programmed in Java is actually running java.exe, a native windows application, that interprets your Java code and creates an application. We call this "running a program on a virtual machine"; in this case we are using a java virtual machine. You can think of Java as an operating system running on top of an operating system (which explains why Java applications usually run much slower than native applications).

Beginner's Note Beginner's Note

To access the command prompt, a window that allows you to type dos commands, on
Windows 95: Start>Programs>Command Prompt
Windows 2000: Start>Programs>Accessories>Command Prompt


Side NoteSide Note Virtual machines such as the Java Virtual Machine, are becomming more common all the time. For instance, Windows 2000 no longer runs on DOS, but some DOS programs may still be used through the integrated Dos Virtual Machine.

Now that we are certain Java is properly installed, we can write our first Java application. This application will simply display the words, as programming tradition dictates, "Hello world!" on the screen. Granted, such an application would not serve much purpose, but it's a great way of learning how to write, compile and run a small program.

To start off with, we must write the source code for our application. These are the instructions, in the plainest english possible, that the java compiler will later transform into machine code, which will in turn be interpreted by the Java virtual machine. All programs ever written on this planet, with the exception of a few simplistic Visual Basic application or code written in assembly, comes from source code. This is the part that you, as a programmer writes and that software companies spend millions writing and protecting (should your source code be stolen, anyone could modify your application and claim it to be their own).

Since source code is simply plain ASCII text, you may use any plain text editor to write it. In this example, we will be using the windows Notepad.

Open Notepad (Start>Programs>Accessories>Notepad) and type the following code, making sure you use the same letter cases:

public class Hello
{
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

Source Code Download
Download a file hello.java (0.112 kb)

Side NoteSide Note When writing Java code, spaces and line breaks have no effect whatsoever. This means that there is absolutely no need to worry about when to put spaces or when to create new lines. The compiler can tell where statements end by looking at the semicolons (;) and will hence ignore all spacing. It is however important to develop your own code writing style so that your code looks elegant and specific parts can be found with ease. One good idea, used by most programmers, is to put indentations (also know as "tabs", since you can usually create them by pressing the tab key) when entering and exeting blocks of code. In this way, it is always easy to tell where all the different blocks of code start and end.

Do not worry about what this code does at the current time, all of these concepts will be explained shortly. For the momment we are only interested in learning how to run an application.

Source code in Notepad
Figure 1.0: Typing source code in Notepad

Save the file, somewhere on your computer. It would be a good idea to create a folder for all of the applications you will be creating while following this guide, and a seperate folder inside that one for each application you write. Call the file "Hello.java". Please note that all source files you produce in the future will always have a *.java extension since that is the extension commonly used for java source code, and the one recognized by the java compiler.

We must now transform this plain text source code into machine code, interpretable by the java virtual machine, by compiling it. To compile the application, navigate to the location where you saved the source code file (by using the dos "dir" and "cd" commands) and simply type:

javac Hello.java

at the command prompt.

Beginner's Note

When using a command prompt, you must navigate through the folders and files located on your computer using good old DOS commands. The two commands necessary to reach the folder where you saved your source code file are:

dir : Lists all the files and folders located in the current directory.
cd name_of_folder : Changes the current folder to the one specified in name_of_folder. Of course, to know what the name of the folder is, you will have to use the dir command, as explained above. If you are using a version of Windows prior to Windows 2000, you will have to include the directory name in quotations ("name_of_folder"), if its name is longer than 8 characters.
cd\ Brings you back to the root directory (which would be c:\ if you are navigating the C drive).
cd.. : Brings you up one directory in the folder hierarchy.

After the few seconds necessary for java to compile the source code, you should be returned to a command prompt if all went well. If any errors are encountered, simply check to see if you have correctly copied the source code. Since even the slightest typing mistake can render your application uncompilable, you may wish to download the source code.

If you check the directory contents, you will notice that java has created a new file named java.class. This is the actual file that will be read by the java virtual machine. Just like the java launcher, the java compiler is also a native windows executable (javac.exe) located in either your windows or system directory. Since the source code is not necessary to run an application that has already been compiled, the java compiler does not need to reside on the end user's computer.

To run the application that has now been compiled, type:

java Hello

at the command prompt, making sure you are still in the directory that contains the compiled Hello world application. You should now see the message "Hello world!" appear on the screen (see figure 1.1).

Hello world console application
Figure 1.1: Hello world output

Next:
Running your first application with VisualCafe