Explain the procedure to write, compile, and run a Java program with simple program.



How to Compile and Run Java Program

In this section, we learn how to compile and run java program step by step.

Step 1:

Write a program on the notepad and save it with .java (for example, DemoFile.java) extension.

  1. class DemoFile  
  2. {  
  3. public static void main(String args[])  
  4. {  
  5. System.out.println("Hello!");  
  6. System.out.println("Java");  
  7. }  
  8. }  

Step 2:

Open Command Prompt.

Step 3:

Set the directory in which the .java file is saved. In our case, the .java file is saved in C:\\demo.

How to Compile and Run Java Program

Step 4:

Use the following command to compile the Java program. It generates a .class file in the same folder. It also shows an error if any.

  1. javac DemoFile.java  
How to Compile and Run Java Program

Step 5:

Use the following command to run the Java program:

  1. java DemoFile  
How to Compile and Run Java Program