Tuesday, June 23, 2009

The "Hello World!" Application

Every Java application contains a class that defines a method called main(). The name of this class is the name that you use as the argument to the Java interpreter when you run the application. You can call the class whatever you want, but the method which is executed first in an application is always called main(). When you run your Java application, the method main() will typically cause methods belonging to other classes to be executed, but the simplest possible Java application program consists of one class containing just the method main(). As you will see below, the main() method has a particular fixed form, and if it is not of the required form, it will not be recognized by the Java interpreter as the method where execution starts.

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


Type the above code using your favorite plaintext editor in the folder you want with the name HelloWorld.java
Bring up a shell, or "command," window. You can do this from the Start menu by choosing Run... and then entering cmd. To compile your source file, change your current directory to the directory where your file is located (in my case is c:\) , if you enter dir at the prompt, you should see your source file.Now you are ready to compile. At the prompt, type the following command and press Enter.
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. Now that you have a .class file, you can run your program. At the prompt, type the following command and press Enter
      java HelloWorldApp
The following image depict the above steps.

No comments:

Post a Comment