Java J2SE Hello World Sample Program

Posted September 4, 2004 by Rex in Java programming

The Hello World program prints Hello World to the screen. It has long been a staple first application for a programmer trying out a new programming language. This tech-recipe describes the steps involved in compiling and running a simple Hello World program in Java.


To complete this program, you must first download a Java JDK on your system. (Check first to see if one is already there. Look for an executable called javac or javac.exe.) Then look for the highest version number that does not end in RC (for release candidate). Make sure to download the JDK (Java Development Kit), not the JRE (Java Runtime Environment). The develepment kit includes the Java compiler. Install the JDK as described in the release notes for your platform.

In your favorite editor, create a file called HelloWorld.java with the following contents:

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

From a command line, the command to compile this program is as follows:

javac HelloWorld.java

For this to work, the javac must be in your shell’s path, or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program.

To run the program, simply use the following Java command:

java HelloWorld

It is important to note that you use the full name with extension when compiling (javac HelloWorld.java) but only the class name when running (java HelloWorld).

The Conversation

Follow the reactions below and share your own thoughts.