Java J2SE Hello World sample program

Contributor Icon Contributed by Rex Date Icon September 4, 2004  
Tag Icon Tagged: Java programming

The Hello World program (one that just prints Hello World to the screen) has long been a staple first application for a programmer to try out a new programming language. This recipe describes the steps involved in compiling and running a simple Hello World program in Java.


You must first download a Java JDK on your system (check first, there may already be one.. look for an executable called javac or javac.exe). 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:

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, you just run it with the 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).

Previous recipe | Next recipe |
 

Viewing 1 Comment

 
close Reblog this comment
blog comments powered by Disqus