hello world java program

Hello World java program :

This page will introduce "hello world" program in java and basics required to run the java program.

Prerequisite :
  • JDK must be installed on machine 
  • must have set the java path
Steps :
  1. Open any editor -
  2. Write java program as below- 
Example of Hello World
class DemoClass {
 public static void main(String[] args) {
  System.out.println("Hello World");
 }
}

3. Save the file with the same name as that of class name with extension .java -> DemoClass.java
4. Open the terminal go to the directory where program file is saved using cd command.
        let say you have saved program file in Program directory - /Users/username/Program
5. Compile program using command - javac  DemoClass.java
       After compiling the file you can see .class file is generated in same directory as DemoClass.class
6. Run program using command command - java DemoClass

    We are DONE! You can see output - Hello World.



 Please note - 
1. We always compile program using the java file name, here file name is DemoClass.java
2. we always run using the class name and not file name, here class name is DemoClass.
In this example, java file name and class name is same.





No comments:

Post a Comment

JSP and Servlet

JSP and Servlet