To install JDK 21 and Visual Studio Code to write and run a “Hello World” Java program, you can follow these detailed steps:
Step 1: Install JDK 21
- Download JDK 21:
- Go to the official Oracle website or an OpenJDK version provider.
- Find JDK 21 and download the installer for your operating system (Windows, macOS, or Linux).
- Install JDK 21:
- Windows:
- Run the downloaded installer.
- Follow the installation prompts. Make sure to note the installation path.
- Add JDK to the PATH environment variable:
- Search for “Environment Variables” in the Start menu.
- Click on “Edit the system environment variables.”
- In the “System Properties” window, click on “Environment Variables.”
- Under “System Variables,” find and select the “Path” variable, then click “Edit.”
- Click “New” and add the JDK
bin
folder path (e.g.,C:\Program Files\Java\jdk-21\bin
). - Click “OK” to close all dialogs.
- macOS/Linux:
- Open Terminal.
- Change to the directory containing the downloaded installer.
- Run the installer using the appropriate command. For macOS, it might be a
.dmg
file that you can simply open and follow the prompts. For Linux, you might use a package manager or run an installer script.
- Windows:
- Verify Installation:
- Open a terminal or command prompt.
- Type
java -version
and press Enter. - If JDK 21 is correctly installed, you should see the version number displayed.
Step 2: Install Visual Studio Code
- Download Visual Studio Code:
- Visit the official Visual Studio Code website (code.visualstudio.com).
- Download the version for your operating system.
- Install Visual Studio Code:
- Windows/macOS: Run the downloaded installer and follow the prompts.
- Linux: Follow the specific instructions for your distribution on the Visual Studio Code website.
- Install Java Extension Pack:
- Open Visual Studio Code.
- Go to the Extensions view by clicking on the square icon on the sidebar or pressing
Ctrl+Shift+X
. - Search for “Java Extension Pack” by Microsoft and install it. This pack includes essential tools for Java development.
Step 3: Write and Run a “Hello World” Java Program
- Create a New Java Project:
- Open Visual Studio Code.
- Click on “File” > “New File”.
- Save the file with a
.java
extension, for example,HelloWorld.java
, in a new folder.
- Write the Hello World Program:
- Type the following code into the new file:javaCopy code
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
- Save the file (
Ctrl+S
).
- Type the following code into the new file:javaCopy code
- Run the Program:
- Right-click in the text editor window and select “Run Java.”
- Alternatively, press
F5
to run the program. - The output “Hello, World!” should appear in the terminal or output window in Visual Studio Code.
Refer video @ https://youtu.be/mpd21PugirQ