How to Run Java Programs from Command Prompt
Java is one of the most commonly used programming languages. It is also an IDE-intensive programming language, tightly integrated with Eclipse. You can run Java programs from the Command Prompt for quick compilation and execution.
If you are just starting to learn Java, this basic guide will help you to start running Java applications from Command Prompt in Windows 10/11.
Install Java Development Kit (JDK) in Windows
Before you can run Java programs on your computer, you need to install a dedicated compiler. This is included in the Java Standard Edition Development Kit (JDK). It is an essential tool for developing in Java on any platform.
The JDK is not the same as the Java Runtime Environment (JRE) that you would install if you had ever used a Java application on your machine.
1. Go to the official page and click on the latest Java SE version to start the download.
Download Java Latest Version
2. Download the JDK from the Oracle website - version for Windows. Download any of the following: x64 installer, x64 archive or x64 MSI installer.
Note : If you are just using simple Java software, make sure you do not download “Java SE Development Kit for Java SE subscribers” on the same download page. If you want to use Java's JRE for Microsoft Windows installation, it has been moved to another page.
Download the latest Java for Windows installer
3. Run the installer as you would for any other program and follow the instructions.
Java Installation Wizard for Windows
Note the location on Windows where Java is being installed. It will be useful later when you are trying to run Java from Command Prompt.
Java Location on PC
4. The installation will finish in just a few seconds. If it takes a long time, close all your other apps from the Task Manager and reinstall the software.
Java installation in progress
5. Finally, you will see the status “Successfully Installed”.
Notice of successful installation of Java
Run Java program from Command Prompt
1. Create a simple Java program like the one below using Notepad or another text editor.
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
2. Make sure to save the file with the extension “.java” instead of “.txt”.
Save Notepad file as Java extension
3. Open Command Prompt with admin rights from the Windows Start menu.
Open Command Prompt with admin rights
4. Use the cd command to change the working directory to the directory containing the Java program. To know which folder to move to, check Java's saved location on PC as discussed above.
cd Documents[thư-mục-chứa-chương-trình-Java]
5. From here, find the path to the version of the Java Development Kit (JDK) on your computer. For example, if you are running 64-bit Windows, it will usually be in “C:\Program Files\Java”
Change directory to Java
6. Next, set the path to the JDK with the set command:
set path=%path%;C:\Program Files\Java\jdk-"Số phiên bản Java".bin
7. You may need to change the directory path to reflect the current Java version. Make sure you are using the Java Development Kit (JDK) folder and pointing to the “bin” folder.
Change directory to Jdk Bin
Note : The Java Runtime Environment (JRE) folder also contains the “bin” folder but does not contain the Java compiler. If you get an error during compilation, make sure you are using the correct directory path.
8. Compile Java program with javac command as shown below. Warn that you won't see anything happen. However, if you use the dir command, you'll notice a new file in the directory ending with the ".class" extension , indicating that the program has been compiled.
javac "Program Name".java
Compile Java program using javac . command
9. Use the java command to run your program:
Java Prog is running
You will see the program run in the Command Prompt window, but there is one more task you can do to make sure the Java program runs smoothly: Set your path.
Set PATH permanently
The above command does not permanently set your Java compiler PATH. It sets an environment variable for that session, but that change is deleted when you close the Command Prompt session.
Permanently setting the Java compiler PATH can be useful if you want your compiled Java programs to run smoothly after your PC restarts. This helps launch the required programs quickly from the Command Prompt window (or a third-party software like Eclipse).
Follow the steps below to change the PATH variable for all future sessions.
1. Open Control Panel > System and Security from the Start menu and click System.
Click System
2. Click Advanced System Settings in the menu.
Click on Advanced System Settings
3. Click the Environment Variables button at the bottom once you switch to the Advanced tab in the System Properties window .
Click the Environment Variables button
4. Select the Path variable , then click the Edit button.
Click the Edit button
5. Click New to add a new folder to the path.
Click New
6. Paste the folder path you used above into the text box. Again, make sure you are using the Java Development Kit (JDK) folder and not the Java Runtime Environment (JRE) folder next to it.
Paste folder path
7. Finally, click OK to make changes.
This article introduces a simple Java program, but you can initialize almost any Java program from the Command Prompt. The process is simple no matter what program you're using.
Check out some more articles:
How to Open Command Prompt on Windows 10, 8, 7, Vista and XP
How to compile a kernel - how on a CentOS system
Check and fix hard drive errors with CMD on Windows
How to find and open files with Command Prompt
NEW TECHNOLOGY ELECTRONIC REPORT
Responsible agency: Union of Science and High-Tech Production and Telecommunications (HTI) - Vietnam Academy of Science and Technology
Editor in chief: Vo Tran
Operate and exploit advertising by iGetz Vietnam Media and Technology Joint Stock Company.
CONTACT
357A Nguyen Thi Dinh, TT. Tan Phu, Dong Nai.
Tel: (+84) 818.337.007/5 Fax: 818.337.007
Email: contact@gozon.xyz