Saltar al reproductorSaltar al contenido principalSaltar al pie de página
  • hoy
How to Run Java Program in Command Prompt (CMD) on Windows 11

Learn how to run a Java program in the Command Prompt (CMD) on Windows 11 with this step-by-step tutorial. Whether you're a beginner or need a quick refresher, this guide will show you how to compile and execute your Java code using the terminal. Follow along to set up your environment, compile your Java file, and execute it in just a few steps!

---

Steps to Run a Java Program in CMD on Windows 11

#### 1. *Install Java Development Kit (JDK)*
Download and install the latest JDK from the [official Oracle website](url)
During installation, ensure to select the option to set the *Java Path* automatically.

#### 2. *Check Java Installation*
Open CMD and type:
```cmd
java -version
```
If installed correctly, it will display the Java version.

#### 3. *Set Up Environment Variables (If Not Already Set)*
Open **Control Panel - System - Advanced System Settings -Environment Variables**.
Under "System Variables," locate `Path` and add the JDK `bin` directory (e.g., `C:\Program Files\Java\jdk-XX.X.X\bin`).

#### 4. *Write Your Java Code*
Open any text editor (e.g., Notepad) and write your Java program. For example:
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
Save the file with a `.java` extension (e.g., `HelloWorld.java`).

#### 5. *Compile the Java File*
Open CMD and navigate to the directory where your `.java` file is saved. Use the `cd` command:
```cmd
cd path\to\your\file
```
Compile the file using the `javac` command:
```cmd
javac HelloWorld.java
```
If successful, it creates a `HelloWorld.class` file in the same directory.

#### 6. *Run the Java Program*
Execute the compiled program using the `java` command:
```cmd
java HelloWorld
```
Output:
```
Hello, World!
```

---

Tips
Ensure the file name matches the public class name in the Java code.
Use `cls` to clear the Command Prompt screen for a clean workspace.
Fix errors by carefully reviewing error messages displayed during compilation.

---

Now you’re ready to compile and run any Java program in the CMD on Windows 11! If this tutorial helped, like, share, and subscribe for more programming tips and tricks.

Categoría

📚
Aprendizaje
Transcripción
00:00Hey guys, in this video we are going to see how we can install Java on our Windows Operating System.
00:04So let's get started and let's see how we can do it.
00:07So first of all we are going to check whether Java is already pre-installed on our Windows Operating System or not.
00:13The check is very simple, you just need to search for cmd in your search bar and then open the command prompt.
00:20And here just type java space hyphen hyphen version and then press enter.
00:26And if it says java is not recognized as an internal or external command, that means java is not installed on your Windows Operating System.
00:35So let's move ahead and see how we can install java.
00:40So for that, open your favorite browser and search for java download and then you will see few results here.
00:46So here just scroll down where it says oracle.com.
00:50So we are going to download and install java from oracle.com.
00:55So just click on the link where it says oracle.com forward slash java forward slash technologies forward slash downloads.
01:03I'm going to click on this link here and then I'm going to just scroll down a little.
01:09And here you can see at the time of making this video java JDK23 is the latest version.
01:16And you can see you can install java on your Linux, macOS and Windows Operating Systems.
01:21I'm going to select Windows Operating System here and I want to download this installer which is 64-bit MSI installer.
01:31So I'm going to click on this link in front of MSI installer which is going to start the download of this JDK23 Windows 64 bin.msi file.
01:44So once this MSI file is downloaded, I'm going to click on this file and let me minimize my browser and it's going to prepare the installer and open this kind of Java SE Development Kit Setup window.
01:57So here on this first window you just need to click on the next button and this will be the location where java will be installed on your Windows Operating System which is inside your C drive.
02:11And then there will be this program files where there is a java folder and then it's going to install this JDK version in that java folder.
02:23If you don't have the good reason to change this location just leave it as default and then click on next.
02:28When you click next here you might see this kind of warning which says do you want to allow this app to make changes on your device.
02:35I'm going to click on yes here which is going to start the installation progress and after a few seconds you will be able to see that the java development kit setup has been finished here.
02:48So once that's finished you can see click next step to access tutorial API documentation and developer guides.
02:57So if you want to see those tutorials or developer guides you can click on the next step button.
03:03Otherwise if you don't want to do that you can click on the close button also.
03:09So let me click on the close button.
03:12That means java installation is finished.
03:15So let's check whether java command is working on our Windows Operating System or not.
03:21So once again we are going to search for cmd in the search bar and open the command prompt.
03:26And we are going to give the command java space hyphen hyphen version here and then this time you will see some kind of output instead of this command is not recognized.
03:41So you can see after the installation of java it says java 23.0.1 is installed on our Windows Operating System.
03:52Now some application also requires the java home environment variables to be set on our Windows Operating System so that they can use the location for our Java installation.
04:04So let's try to set the java home environment variable in our Windows Operating System so for that just search for environment on our search bar and then you will see this kind of result.
04:19You can just write few letters of environment and you will see this kind of result which says added the system environment variable.
04:26Click on this result here click on the environment variables button and then under user variables for your username click on the new button and then give the environment variable name which is java underscore home in our case.
04:46Just type the exact same spelling in capital letters java underscore home and the variable value will be the path to our java jdk folder.
04:59So for that we can click on our folder explorer and then click on this pc and then go to your c folder where you will see the program files folder.
05:12So just click on program files here and then you have this java folder here and under java folder you will see jdk23 which is what we have installed so just click on this path and then this is the path you need to copy.
05:28So it's c program files then java and then whatever java version you have installed just go inside that folder and then copy this path until the jdk and the version of your jdk and then the value will be this path for the java home.
05:49So once you are done click on ok and then once again click on ok and then click on ok.
05:56So now java home is set on your windows operating system.
06:00So to check that let me just close this command prompt and open the new command prompt window and here you just need to type echo space percentage java underscore home percentage and then press enter and it's going to show you the path of your java jdk folder which is jdk-23 in our case.
06:21So we have successfully set the java home environment variable also now let's try to compile a java program and see if everything is working related to java compilation on our windows operating system or not.
06:38So for that we are going to create a java file so i'm going to go to my folder explorer and then go to my documents folder where i have created this java apps folder.
06:51If you don't have this folder you can create any folder of your choice with any name and here we are going to create a new java file.
07:00So for that first of all you can click on view here and then click on show and then you need to enable this file name extension option here.
07:11So if this checkbox is there in front of file name extension that's ok.
07:16If it's not there just click on it once and then file name extension will be visible.
07:21So when you right click here and then click on new and then click on the text document here and let's say i want to give the java file name as hello world here and then the extension will be instead of txt
07:40i'm going to name my extension as dot java and then press enter it's going to give me this kind of warning so i'm going to click on yes and you can see this hello world dot java file is created right.
07:54So once this java file is created i'm going to open it with the notepad but you can open it with any editor of your choice.
08:02For simplicity i'm just showing the notepad because it comes pre-installed with your windows operating system here i'm going to just paste a very simple hello world java program the important thing to note here is that this class name and the file name should be same right so my file name is hello world and the class name is also hello world here here we have one main method
08:32where we are using system dot out dot print line to print hello world once that's done just save this file and then close this file and then copy this path here and then we are going to open the command prompt so just search for cmd and then click on the command prompt and then write cd and then paste the path which you have copied for your java apps folder right so our java apps folder is here so i'm i have just copied
09:01this path here so cd and then cd and the path in which your java file is there and then to compile your java file you use this command which is javac command right not java command but javac command and then the name of your java file which is hello world.java in our case now let me uh just move this uh command prompt and this folder side by side
09:31so you can see the generation of java so you can see the generation of output inside this folder explorer here so when i press enter here for this command java c and the java file name is going to create the output of my java file which means it will compile my java program and create the executable file for me and the executable file name is hello world.class right
10:01java program to run this i just need to use the java command now so just write java and then
10:10hello world you don't need to give this dot class extension you just need to write java and then
10:17the hello world without any extension and then press enter and it's going to print the hello
10:22world output that means java compilation and java program execution is working properly on our
10:30windows operating system also so this is how you can install and set up java on our windows operating
10:36system i hope you've enjoyed this video and i will see you in the next video

Recomendada