Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   2. Getting Started  >   2.2. Hello World program

2.2. Hello World program
Last updated: 27 January 2013.

This tutorial will show you how to write, compile and run a basic program that prints "Hello World !" to the standard output. The standard output is the command prompt (for Windows users) or terminal (for Linux and Mac users). Just follow these 5 steps:

1. Open your favorite text editor and type in or copy/paste the class HelloWorld shown below. Any text editor will do the job:You might prefer to use an IDE (Integrated Development Environment) like Eclipse but if you are new to Java, the best way to learn the basics is to use a simple text editor. Eclipse will be introduced later in the intermediate section.

Java is case sensitive. Consequently, the program below must be written exactly as is.

class HelloWorld {

   
// My first Java program

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

}

2. Save the file as HelloWorld.java in the directory of your choice. Don't save it as helloworld.java because the file must have the same exact name as the class that it contains.

3. If you are using Linux or Mac, open a terminal and change to the directory where the file HelloWorld.java resides. Likewise, if you are using Windows, open a command prompt and change to the directory where the file HelloWorld.java resides.

4. Compile the program by typing javac HelloWorld.java at the command prompt or terminal. If there are no compilation errors, the prompt does not print anything and a file named HelloWorld.class is generated in the same directory where the file HelloWorld.java resides. If there are compilation errors, make sure that you wrote the program exactly as above. Java being case sensitive, if you type system instead of System, your code will not compile.

5. Run the HelloWorld.class file by typing java HelloWorld (without the ".class" extension) at the command prompt or terminal. The standard output shows:

Hello World !


Code explanation

You are here :  JavaPerspective.com  >   Beginner Tutorials  >   2. Getting Started  >   2.2. Hello World program
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7