Run Java Programs

How to Run Java Programs in Command Prompt

Java is the most widely used programming language that is dynamic and can be operated on any platform. You’re in the right place if you’re new to Java or just wish to use the command prompt to run Java apps. Running the javascript program on different operating systems are same. In this article, we will guide you on how to run Java using the command prompt in step by step manner.

How to Run Java Programs in Command Prompt


Before you can run Java programs from the command prompt, you’ll need to ensure you have the following:

  1. Java Development Kit: The Java compiler (javac) and other crucial tools needed for Java development are included with the JDK. The most recent JDK version is available for download from the official Oracle website, or you can pick an open-source substitute like OpenJDK.
  2. Text Editor: To create and save Java source code, we need a text editor. Most Commonly used text editors are Visual Studio Code, Notepad (which is included with Windows), and any other code editor of your choice.

Setting Up the Environment

We need to set up our environment variables once we have the JDK installed and a text editor available:

  • Install JDK: If you haven’t installed JDK, you can just download it from the official site and follow the installation according to the instructions provided along the way.
  • Environment Variable: We need to set the environment variable JAVA_HOME to the location of the JDK’s installation directory. Follow the given below steps:
    • Select “Properties” by right-clicking on “This PC” (Windows 10).
    • On the left, select “Advanced system settings”.
    • At the bottom of the Advanced tab, select the “Environment Variables” button.
    • Scroll down to the “System Variables” area, find the Path variable, select it, and then click on “Edit.”
    • Select “New” and provide the path to your JDK installation directory (e.g., ‘C:\Program Files\Java\jdk1.8.0_291’) in the JAVA_HOME variable’s name. Make sure to modify the path to reflect the version of your JDK.
  • Updating the path variable: Inside the “Environment Variables” window, find the Path variable in the “System Variables” section, select it, and click “Edit.”
    • Add ‘%JAVA_HOME%\bin’ to the list of paths. It will help in making sure the system can easily find Java executable files.
  • Verifying Setup: Search the command prompt run it as an administrator and run the following command:
How to Run Java Programs in Command Prompt

Writing a Simple Java Program:

It is time to create a basic Java program. Create a new file with the extension “” in your text editor. Name the files a “”.

How to Run Java Programs in Command Prompt

Save the file in the directory of your choice.

Compiling and Running Your Java Program

Follow these steps to compile and run our Java program:

How to Run Java Programs in Command Prompt
  • To compile your Java program, use the javac command.
How to Run Java Programs in Command Prompt

This command will create a HelloWorld.class file in the same directory if your code is error-free.

  • After compiling successfully, use the Java command to run your Java program:
How to Run Java Programs in Command Prompt

Woohoh! You’ve successfully written, compiled, and run a Java program from the command prompt.


Java developers must have a basic understanding of how to run Java programs in the Command Prompt. You can write, compile, and run Java programs without difficulty in a command-line environment by setting up your environment variables by following all of the steps listed above.

If you find this Blog helpful, then make sure to search Codewithrandom on Google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Follow: CodewithRandom
Author: Arun

Leave a Reply