Compile & Execute Basic Java Program in Command Prompt |First Java Program | How to Run Java Program

Published: 17 May 2024
on channel: Engineering Unplugged
56
2

Here's a detailed description for a tutorial on compiling and executing a basic Java program using the command prompt:

Compiling and Executing a Basic Java Program in Command Prompt

In this tutorial, we will walk you through the process of writing, compiling, and executing a basic Java program using the command prompt. This fundamental skill is essential for understanding how Java code is transformed into executable programs.

#### Steps Covered:

1. **Writing Your First Java Program**:
Open a text editor and write a simple Java program.
Save the file with a `.java` extension.

Example:
```java
// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```

2. **Setting Up the Environment**:
Ensure that the Java Development Kit (JDK) is installed on your system.
Verify the installation by running `java -version` and `javac -version` in the command prompt.

3. **Compiling the Java Program**:
Open the command prompt.
Navigate to the directory where your Java file is saved.
Compile the program using the `javac` command.

Command:
```sh
javac HelloWorld.java
```

This command will generate a `HelloWorld.class` file in the same directory.

4. **Executing the Java Program**:
Run the compiled Java program using the `java` command.

Command:
```sh
java HelloWorld
```

You should see the output: `Hello, World!`

#### Key Points:
**File Naming**: Ensure your Java file name matches the public class name.
**Directory Navigation**: Use `cd` command to change directories in the command prompt.
**Compilation**: The `javac` command converts your `.java` file into bytecode (`.class` file).
**Execution**: The `java` command runs the bytecode on the Java Virtual Machine (JVM).

By following these steps, you'll learn how to manually compile and run Java programs, a crucial step in understanding the Java development process.


On this page of the site you can watch the video online Compile & Execute Basic Java Program in Command Prompt |First Java Program | How to Run Java Program with a duration of hours minute second in good quality, which was uploaded by the user Engineering Unplugged 17 May 2024, share the link with friends and acquaintances, this video has already been watched 56 times on youtube and it was liked by 2 viewers. Enjoy your viewing!