How to run Java Program in cmd (Command Prompt)


In this article, we will learn how to run the Java Program in cmd / using command prompt.

We are using Java compiler javac for compiling the Java program and Java interpreter java for running the Java program.

Let’s get started :

How to run a Java Program using CMD

We need to follow below mentioned steps in order to run the Java in CMD :

  • Create a folder in your machine.
  • Create one Java class with some logic.
  • Open the command prompt or cmd in your machine.
  • Run the Program in CMD.

Create a folder

let’s create a folder named as Sample in your C drive of your machine.


Create a Simple Class

public class Test{

    public static void main(String[] args){
       System.out.println("Sample Class to test");

Let’s save this class with name – in the Sample folder.

Open Command Prompt / CMD

Open command prompt in your machine. You can also open it by typing cmd in run window.

Run the Java Program in CMD

Below are the required steps in cmd :

Follow the below steps:

C:\Users\Admin> cd\
C:> cd Sample

Now the current directory is Sample.

C:\Sample> set path=%path%;C:\Program Files\Java\jdk1.8.0_101\bin

Replace the above JDK version with your installed java version.

C:\Sample> javac

This will Compile the program in cmd.

C:\Sample> java Test

This is how to run the program in cmd using interpreter.

It will print the following output :

Output:Sample Class to test

Additional Points

You need to ensure that your Windows is able to find the Java compiler and interpreter or not.

Follow below steps :

  • Go to Start -> Computer -> System Properties -> Advanced System Settings -> Environment Variables -> System Variables -> PATH
  • Click on the β€œEdit” button.
  • Append the semicolon and then jdk path at the end.
  • Click on OK button.


We have learnt how to run java program in cmd. It is a very basic question yet very important. To install java in your machine, you can visit url.

You can check your java version with following commands in cmd :

java -version

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply