Getting Started: Hello World Application

Environment setup on windows machine


Download latest Java SE development Kit 7(JDK 7)
You can download the Windows version JDK (http://www.oracle.com/technetwork/java/javase/downloads/index.html).
Download and install

Environment variable

  1. Select Computer from the start menu
  2. Choose System Properties from the context menu
  3. Click Advanced system settings > Advanced tab
  4. Click on Environment Variables, under System Variables, find PATH, and click on it.
  5. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
  6. Reopen Command prompt window, and run your java code.

Creating Your First Application


Your first application, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will:

Create a Source File

Open Notepad editor new document  type in the following code:
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

Save the code in a file with the name HelloWorldApp.java. To do this in Notepad, first choose the File > Save As menu item. Then, in the Save As dialog box:
  1. Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is java on the C drive.
  2. In the File name text field, type "HelloWorldApp.java", including the quotation marks.
  3. From the Save as type combo box, choose Text Documents (*.txt).
  4. In the Encoding combo box, leave the encoding as ANSI.
When you're finished, the dialog box should look like this.
TEXT The Save As dialog, as described in the text.


Now click Save, and exit Notepad.

Compile the Source File into a .class File

Open command prompt.

a window where you can enter DOS commands


Go to project folder where your program is saved. c:/java/HelloWorldApp.java

Changing directory on an alternate drive


Directory listing showing the .java source file.


Compile java program with "javac" compiler.
e.g: javac HelloWorldApp.java

The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.


Directory listing, showing the generated .class file


Now that you have a .class file, you can run your program.

Run the Program

In the same directory, enter the following command at the prompt:
java HelloWorldApp
The next figure shows what you should now see:

The program prints Hello World! to the screen.

Congratulations! Your program works!

Post a Comment

0 Comments