How to Run a Java Program from the Command Prompt

Java is one of the most commonly used programming languages. It is also an IDE-intensive programming language, with tight integration with Eclipse. If you are just starting out learning Java, here is a basic guide to help you start running the Java application from the command prompt (in Windows).

Installing the Java Development Kit (JDK)

Before you can run a Java program on your computer, you’ll need a Java compiler installed. That comes with the Java Development Kit, or JDK. It’s an essential tool for developing in Java on any platform. The JDK is not the same as the Java Runtime Environment, or JRE, which you’ll already have installed if you’ve ever used a Java application on your machine.

1. Download the JDK from Oracle’s website. Make sure not to download the JRE to Server editions.


2. Make sure you grab the Windows version of the development kit.


3. Run the installer normally.

Running a Java Program from the Command Prompt

1. Create a simple Java program like the one below using Notepad or another text editor. Make sure to save the file with the extension “.java” rather than “.txt.”

2. Open the Command Prompt from the Start Menu. You can also press Win + R, then type cmd.exe into the Run field.


3. Use the cd command to change your working directory to the directory containing your Java program. Mine is saved in my “Scripts” folder, but you can save your file anywhere you’d like on your system.


4. Locate the path to the version of the Java Development Kit (JDK) on your computer. If you’re running 64-bit Windows, that will typically be in “C:\Program Files\Java.”


5. Set the path to the JDK with the “set” command, as seen below:


You may need to change the directory path to reflect the current version of Java. Make sure you’re using the Java Development Kit (JDK) directory and pointing to the “bin” folder. The Java Runtime Environment (JRE) folder also contains a “bin” folder but doesn’t hold the Java compiler. If you get errors around the compilation, make sure you’re using the correct directory path.

6. Compile the Java program with the javac command, as seen below:


7. You should see absolutely nothing happen. But if you use the dir command, you’ll notice a new file in your directory ending in the “.class” extension. This indicates the program has been compiled.


8. Use the java command to run your program:


Setting Your PATH Permanently

The above command doesn’t set your Java compiler PATH permanently. It sets the environmental variable for that session, but that change will be wiped away when you close the Command Prompt session. Follow the steps below to change your PATH variable for all future sessions.

1. Open Control Panel from the Start Menu and click “System.”

2. Click “Advanced System Settings” in the left menu pane.


3. Click the “Environment Variables…” button at the bottom of the System Properties window.


4. Select the “Path” variable, then click the “Edit” button.


5. Click “New” to add a new directory to your path.


6. Paste the directory path you used above into the text box. Again, make sure you’re using the Java Development Kit (JDK) directory and not the Java Runtime Environment (JRE) directory next to it, then click “OK” to commit to the change.



We ran an extremely simple Java program in our example. But you can initiate Java programs of greater complexity from the Command Prompt. Just use javac to compile a file ending in “.java,” then use the java command to execute the compiled program.

Alexander Fox Alexander Fox

Alexander Fox is a tech and science writer based in Philadelphia, PA with one cat, three Macs and more USB cables than he could ever use.

One comment

  1. am new to java and i have watched a video about compiling with java 1.4….but i just installed java 9.0 and i don’t know where to save the demo file so that i can compile and run it with my command prompt.


Comments are closed.