Visual Studio Code (VS Code) is a popular code editor that supports multiple programming languages, including Java.
Setting up Java in Visual Studio Code allows you to develop and debug Java applications efficiently. In this tutorial, we will guide you through the steps of setting up Java in Visual Studio Code on Windows.
Step 1: Install Java Development Kit (JDK)
Before you can start programming in Java, you need to have the Java Development Kit (JDK) installed on your computer.
The JDK includes the Java Runtime Environment (JRE), the Java compiler, and other tools needed for Java development.
To install the JDK, follow these steps:
- Go to the Oracle JDK download page: https://www.oracle.com/java/technologies/javase-jdk16-downloads.html
- Accept the license agreement and download the appropriate JDK version for your operating system.
- Run the downloaded installer and follow the prompts to complete the installation.
- Once the installation is complete, open the Command Prompt and type
java -version
to verify that Java is installed correctly.
Step 2: Install Visual Studio Code
Visual Studio Code is a free and open-source code editor developed by Microsoft. It is available for Windows, Linux, and macOS.
To install Visual Studio Code, follow these steps:
- Go to the Visual Studio Code download page: https://code.visualstudio.com/download
- Download the appropriate version for your operating system.
- Run the downloaded installer and follow the prompts to complete the installation.
- Once the installation is complete, open Visual Studio Code.
Step 3: Install Java Extension Pack
The Java Extension Pack is a collection of popular extensions for Java development in Visual Studio Code. It includes extensions for language support, debugging, code formatting, and more.
To install the Java Extension Pack, follow these steps:
- Open Visual Studio Code.
- Click on the Extensions icon on the left-hand side of the screen.
- In the search bar, type “Java Extension Pack” and press Enter.
- Click on the “Install” button next to the Java Extension Pack.
- Wait for the installation to complete.
Step 4: Create a Java Project in Visual Studio Code
Now that you have Java and Visual Studio Code set up, you can create your first Java project.
To create a Java project in Visual Studio Code, follow these steps:
- Open Visual Studio Code.
- Click on the “Explorer” icon on the left-hand side of the screen.
- Click on the “New Folder” button to create a new folder for your project.
- Give your project a name and press Enter.
- Right-click on the new folder and select “New File”.
- Give your Java file a name, with the extension “.java”.
- Enter the following code into your Java file:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
- Save the file by pressing Ctrl + S.
Step 5: Run the Java Program
To run your Java program, follow these steps:
- Open the Command Prompt.
- Navigate to the directory where your Java file is located using the
cd
command. - Compile your Java file by typing
javac filename.java
and pressing Enter. - Run your Java program by typing
java filename
and pressing Enter. - The output “Hello, World!” should appear in the Command Prompt.
Conclusion
In this tutorial, we have shown you how to set up Java in Visual Studio Code on Windows. By following these steps, you can start developing and debugging Java applications efficiently using Visual Studio Code.