While we will be talking exclusively about Command Prompt, this functionality exists for its macOS counterpart, Terminal, too. The process is the same for Mac as it is for Windows. As a prerequisite, you’re required to download a dedicated Java compiler on your computer. You can download the Java Development Kit (JDK), which runs across platforms as the most important toolkit to writing Java code, from the Oracle website. You’ll have to have the JDK downloaded and installed on your computer before you can begin the process.

How to run a Java program using Command Prompt

First things first, you will have to write the code that you want to run and save that as a Java executable file. For this purpose, you can use either MS Word or Notepad. For example, we’ll be writing a simple code that gives the output “Hello World!”. This is what the code would look like: Users should make sure of proper indentations since neither Notepad nor Word would help you with that, and also that the file is saved as a ‘.java’ file and not as a ‘.txt’ file. In order to do so, name your file as ‘SampleFile.java’ (You can name the file anything, as long as the file extension is .java and from File type, select ‘All Files’. This file is to be saved in a separate folder in whichever destination you prefer. Now, open the Command Prompt and run it as administrator. We will make use of the ‘cd’ command to change the operating directory on the Command Prompt to the one where your Java program is stored. Run the following command line:

You can check if your Java program file is present in this directory by using the Dir command too. Now, locate the path of the JDK on your computer, and set the path to the JDK via the following command line. For example, if you’re running the 64-bit Windows version (since that’s most commonly the case), and you decide to install the 64-bit JDK file, your folder destination will be “C:\Program Files\Java”, unless you decide to change it during installation. Next up, use the javac command (Java compile) to compile your program.

You have to follow the javac keyword after the filename that you’ve chosen for your program. You won’t see anything happen just yet, since the program has only been compiled and not run yet. Finally, to run the command, use the Java command. If you’re compiling simple programs and housing them all in the same directory, you don’t have to set a permanent path for your files, but if the case is otherwise, then you will be better off doing so. The Command Prompt sets the environmental variable for that session, but those settings are forfeited once you close Command Prompt and your session is timed out. Thus, if you want to set a permanent path for all your future sessions, you can follow the steps below: Open the Control Panel and visit the System and Security settings. You can locate them by viewing Control Panel settings as Category. This page can, alternatively, be approached via the Windows settings too. From the left-side options pane, select Advanced System Settings and click on the Environment Variables button on the bottom of the System Properties box.

Under the System Variables, you will find the Path variable. Select that and click on Edit, which is right below it.

In the Edit Environment Variable box, click on New, which will allow you to add a new directory to your path.

Here, paste the path that you’ve used above while compiling the Javascript and save the settings by clicking on Ok. These are steps you have to follow in order to compile and run a Java program using the Command Prompt. As a side note, users should keep in mind to use the Java Development Kit for the procedure and not the Java Runtime Environment, which is something probably most Java programmers, who are reading this article, already have installed and running on their computers. You can download the JDK from Oracle.com. We hope that this article was able to help you.