If you are reading this post on How to Run Java Program In Cmd then it seems like you have already written a java program.

There are lot’s of programming environments which will provide you platform to run and compile java programs.

But there is also an alternate way to run a java program i.e using cmd prompt. Windows have a command prompt and similar IOS also has its own command prompt called as Terminal.

The command prompt is the platform situated in windows operating system where you can execute the commands.

Before running the java program in cmd prompt there must be JDK(java development kit) installed in your PC. Without JDK it’s impossible to compile and run java program in cmd prompt.

To run a java program in the command prompt you will need to do two main steps:Install JDk in your PC.

  1. Install JDK in Your PC.
  2. Set the Path of JDK in Computer Settings.

So, first of all, we will show how to download JDK in your PC.

How to Download JDK in Your PC.

1st Step: Visit Oracle’s official website and search for JDK.

2nd Step: Find a Suitable Version of Java JDK SE Development Kit.

3rd Step: Make sure you grab the windows version.

4th Step: Now Download the JDK and install it in your PC.

Now finally JDK is installed in our computer Now let’s learn how to run a java program in Cmd using notepad.

How to Set Path for JDK

1st Step: Right Click on My Computer or This PC and click on Properties.

2nd Step: Click on Advanced System Settings. Now Inside Advanced Tab click on Environment Variables option.

3rd Step: A window will appear So in System Variable click New. Now give variable name as “var” and In the path, field put the path of JDK C:\ProgramFiles\Java\jdk1.8.0_251\bin.

4th step: Click OK.

Now the Path has been set.

To run a java program, your program must be programmed in a Microsoft‘s inbuilt Application called Notepad.

Before start learning let us consider an example of a java program. We will take an example of a Hello World java program.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>class test
{
public static void main(String args[])
  {
  System.out.println("Hello World!");
  }
}</pre>
</div>

The Output of the above java example is “Hello World!“.

So we will see how to compile this above code in Cmd using Notepad.

Steps to Run Java Program In Cmd.

1st Step: Type the above same java code in the Microsoft inbuilt application called Notepad as shown in the below image.

how to run java program in cmd

2nd Step: Now save the Notepad file in a specified folder anywhere you want. while saving the file the extension of that file should be “.java” example test.java as shown in below image.

how to run java program in cmd

3rd Step: Now open the command prompt from the menu. Don’t directly open the command prompt. First right-click on command prompt and then click Run as administrator as shown in the image.

how to run java program in cmd

After clicking on Run as administrator the command prompt will be opened on your screen.

command prompt

4th Step: Now using cd (command directory) command, change the location of the working directory. In my pc, I have saved a java file in location C->users->btbj->test program->test.java.

Now we will locate the exact java file location using command prompt.

how to run java program in cmd using notepad

We have located the exact java file in the command prompt.

5th Step: Now compile the program using java compiler command “javac”. For compiling the test java file type javac(yourfilename.java) and press enter.

how to run java program in cmd using notepad

After compiling the java file the .class file has been added extra. To check whether the .class file has been added or not type dir in command prompt.

using dir command in cmd

6th Step: Now run the program using the java command i.e type “java yourfileclassname” and press enter. Now the compiler has been displayed the output on the screen i.e Hello World!.

running the java code

How to Run Java Program In Windows 10

1st Step: Type the java code in notepad and save in a specific folder with extension .java.

2nd Step: Now open the command prompt.

3rd Step: Set the path of JDK and locate the file on command prompt.

4th Step: compile the file using java compiler command javac yourfilename.java.

5th Step: After compiling the file run the program using command java classname and press enter.

6th Step: Now the output is printed on the screen.

How to Run Java Program in cmd Youtube

In this Youtube video, we have shown how to run the java program in cmd Using Notepad. This video covers setting the path of JDK and the process of compiling and running the java program link.

Run Java Program Online

Did you know? the java program can also be compiled online. There are also online java compilers available. OnlineGDB is the best online compiler that I will personally recommend.

Run Java From Command Line Linux

OpenJDK is best for working in Linux. I never faced any problem using it in Linux.

To Run Java from command Line in Linux Just follow the steps:

1st step: With the help of terminal install open JDK using the command “sudo apt-get install openjdk-7-jdk”

2nd step: Now write the java code in Text Editor and save in the root with filename.java extension.

3rd step: Now compile the program using java compiler javac i.e type javac filename.java in terminal.

4th step: Now the new class file has been created.

5th Step: Now run the java file by typing java classname in the terminal.

6th Step: Now the output has been displayed on the screen.

Conclusion

So we have seen how the java program is run and compiled in the command prompt. Hope you have learned and understand how to run a java program in cmd using notepad. If you have any doubt and query then please feel free to ask in the comment section below.

Also Read: