Three things need to be done to make a java program work. They are,
1. Create,
2. Compile, and
3. Run

  • A source file is to be created with .java extension.
  • The source file is then compiled by the java compiler which yields a class file with .class extension
  • This class file is run by the JVM and yields the result.
  • A class file, if it is created in one platform can run and give the same result in another platform, for example, a class file generated in a windows environment can run and give the same result in linux environment. That is why, java is platform independent.

Java Hello World Example Program:


HelloWorld.java

Output:

C:\> javac HelloWorld.java
C:\> java HelloWorld
Hello World

1. Creating a source file:

Type the above program in a notepad and save the file with the name that should match exactly the class name and with .java extension. That is HelloWorld.java

2. Compiling the source file:

Open command prompt and go to the directory where HelloWorld.java is located and type the command
javac HelloWorld.java
The program will be compiled by javac compiler and HelloWorld.class file is created and ready to be executed.

3. Run the program:

Then, type the command.
java HelloWorld
This command will execute the class file and displays the output ‘Hello World’ .

0 Comments:

Post a Comment