How to Run a Java Program from the Command Prompt

A woman sitting at a computer.

Java is one of the most commonly used programming languages. It is also an IDE-intensive programming language, with tight integration with Eclipse. You can run Java programs from the Command Prompt for quick compiling and execution.

If you are just starting to learn 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 dedicated compiler installed. This comes within the Java Development Kit (JDK). It’s an essential tool for developing in Java on any platform.

The JDK is not the same as the Java Runtime Environment (JRE), which you’ll already have installed if you’ve ever used a Java application on your machine.

First, download the JDK from Oracle’s website – the Windows version. Make sure not to download the “JRE to Server” editions.

The Oracle JDK home page.

Next, run the installer as you would for any other program and follow the instructions.

Running a Java Program from the Command Prompt

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.”

Next, open the Command Prompt from the Windows Start Menu.

The Windows Start Menu.

Use the cd command to change your working directory to the directory containing your Java program.

From here, locate the path to the version of the Java Development Kit (JDK) on your computer. For example, if you’re running 64-bit Windows, that will often be in “C:Program FilesJava.”

Next, set the path to the JDK with the set command:

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.

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

Be warned that you’ll see nothing happen. Though, 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.

Finally, use the java command to run your program:

You’ll see the program run within the Command Prompt window, but there’s one more task you can do to make sure your Java program runs smooth – settings your path.

Setting a Permanent PATH

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 -> System and Security” from the Start Menu and click System.

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

The Advanced System Settings.

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

java-program-change-path-002

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

java-program-change-path-003

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

java-program-change-path-004

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.

Lastly, click OK to commit to the change.

Conclusion

This article featured a simple Java program, but you can initiate almost any Java program from the Command Prompt. The good news is the procedure is straightforward regardless of the nature of your program.

If you’d like to understand the difference between Java and JavaScript, we have the perfect article for you. Will this article help you run Java programs from the Command Prompt? Let us know in the comments section below!

Tom Rankin Tom Rankin

Tom Rankin is a quality content writer for WordPress, tech, and small businesses. When he's not putting fingers to keyboard, he can be found taking photographs, writing music, playing computer games, and talking in the third-person.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.