Ankaj Gupta
August 06, 2020

How to run java program | compile and run java program

Run a Java Program using Command Prompt

Running a java program is very easy after JDK(Java Development Kit) installation. Following are the steps −

Step 1 : It is very important to save the written Java program as .java extension(eg : MyProgram.java).

  ➤ Example : MyProgram.java;

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

Step 2 : Open a command prompt window and go to the directory where you saved the java program, We assume that you have created a file on the desktop.

Step 3 : To move to the desktop, type the command below and press enter.

  • cd desktop

Step 4 : Type 'javac MyProgram.java' and press enter to compile your code.

  • javac MyProgram.java

Note : If there are no errors in your code, the command prompt will take you to the next line.

Step 5 : Now finally, type 'java MyProgram' and press enter to run your program.

  • java MyProgram

Now, you will be able to see the result printed on the window.

Java program Java programming

Join the discussion

Comments

0 comments

No comments yet — be the first to share your thoughts.

Related Posts