VisualStudioTutor.com Java Tutorial All Lessons

Lesson 3 of 20

Visual Studio Code Setup

Configure Visual Studio Code for Java, create a project, run code, and use the debugger.

Learning objectives

  • Install the Java tooling required by Visual Studio Code.
  • Open a Java project folder correctly.
  • Run and debug a Java class.
  • Use breakpoints and inspect variable values.
  • Understand a simple project folder structure.

1. How VS Code supports Java

Visual Studio Code is a lightweight editor. Java language support is added through extensions. A common beginner setup is the Microsoft Extension Pack for Java, which bundles language support, debugging, testing, Maven support, and project management tools.

Example project structure

java-vscode-demo/
└── src/
    └── App.java

For small exercises, a simple src folder is enough. Later projects may use Maven or Gradle and a more formal directory structure.

2. Create your first VS Code Java project

  1. Open Visual Studio Code.
  2. Open the Extensions view and install the Java extension pack.
  3. Choose File → Open Folder and open a new folder named java-vscode-demo.
  4. Create a src folder and add App.java.
  5. Type the program below.

Example: calculate a rectangle area

public class App {
    public static void main(String[] args) {
        double width = 8.5;
        double height = 4.0;
        double area = width * height;

        System.out.println("Width: " + width);
        System.out.println("Height: " + height);
        System.out.println("Area: " + area);
    }
}

This example gives VS Code enough Java syntax to demonstrate IntelliSense, variables, arithmetic, and debugging.

3. Run the program

When the Java extensions recognize the class, VS Code normally displays Run and Debug links above main. You can also use the Run and Debug panel.

Expected output

Width: 8.5
Height: 4.0
Area: 34.0

4. Debug with a breakpoint

Click in the left margin next to the line double area = width * height; to create a breakpoint. Start debugging. When execution pauses, inspect width, height, and area. Then use Step Over to continue one statement at a time.

Example debugging question

Change height to 6.0. Before the area line executes, what value should area have? After stepping over the line, what value does it contain? This simple prediction habit is one of the best ways to learn debugging.

Common setup problems

ProblemWhy it happensHow to fix it
VS Code says no Java runtime is foundThe JDK is missing or not visible to VS Code.Verify java -version and javac -version, then restart VS Code.
Run/Debug links do not appearThe Java extension is still loading or the file is outside an opened folder.Open the project folder, wait for Java initialization, and check the Extensions view.
Class name and file name differA public Java class must match its file name.Use public class App in App.java.

Mini exercise

Add a variable named perimeter and calculate 2 * (width + height). Use a breakpoint to inspect the result before it is printed.

Summary

You have a working Java environment in VS Code and know how to run, debug, and inspect a program. Lesson 4 repeats the same core workflow in IntelliJ IDEA so you can choose the IDE you prefer.