How to Run Java Program in Command Prompt (CMD) in Windows 11

Pubblicato il: 16 gennaio 2025
sul canale di: ProgrammingKnowledge2
1,970
19

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](https://www.oracle.com/java/technolog....
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.

#JavaProgramming #JavaInCMD #Windows11Tips #JavaTutorial #RunJavaProgram #JavaForBeginners


In questa pagina del sito puoi guardare il video online How to Run Java Program in Command Prompt (CMD) in Windows 11 della durata di ore minuti seconda in buona qualità , che l'utente ha caricato ProgrammingKnowledge2 16 gennaio 2025, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 1,970 volte e gli è piaciuto 19 spettatori. Buona visione!