Guides for SE student projects »

Caution: VS Code guides section is a work-in-progress, containing content contributed by students.

VS Code - Creating a New Java Project

This guide will walk you through creating a new Java project in VS Code from scratch.

Prerequisites

Need help with following prerequisites? Check out our Preparing VS Code for Java guide first.

  • VS Code with the Extension Pack for Java installed
  • Java Development Kit (JDK) installed

Creating a new project

There are two methods you can use to create a new Java project:

  • Method 1: Use VS Code's project wizard
  • Method 2: Create project manually

Method 1: Use VS Code's project wizard

The easiest way to create a new Java project is using VS Code's built-in project wizard.

  1. Open the Command Palette: / Ctrl+Shift+P | Cmd+Shift+P
  2. Type Java: Create Java Project and select it VS Code Command Palette "Java: Create Java Project"
  3. Select the desired build tool:
    • No build tool - For simple projects
    • Gradle - For projects that will use Gradle build system
    • Maven - For projects that will use Maven build system
  4. Choose the location and enter the project name as prompted
  5. VS Code will create the basic project structure for you

Method 2: Create project manually

If you prefer more control over the project setup, you can create a project manually.

  • Step 1: Create a workspace folder

    • Create a project folder on your computer (e.g., MyJavaProject)
    • Open VS Code and select FileOpen Folder...
    • Navigate to and select your project folder
    • Click "Select Folder" to open it as a workspace

  • Step 2: Set up the project structure

    Create the following simple folder structure:

    MyJavaProject/
    └── src/
        └── (your Java source files will go here)
    
    • Open the Explorer panel: / Ctrl+Shift+E | *md+Shift+E`

    • Right-click in the Explorer panel

    • Select New Folder

    • Name it src

      VS Code automatically handles compilation, so you do not need separate folders for compiled .class files.

  • Step 3: Create your first Java class

    • Right-click on the src folder

    • Select New File

    • Name it HelloWorld.java

    • Add the following code:

      public class HelloWorld {
          public static void main(String[] args) {
              System.out.println("Hello, World!");
          }
      }
      
    • Save the file: / Ctrl+S | Cmd+S

      VS Code will automatically compile the Java file when you save it.

Configuring your JDK

If you already have Java installed, VS Code should automatically detect and configure it when you create your project.

After creating your project, you may need to verify and configure VS Code to use the correct JDK version.

  1. Open the Command Palette: / Ctrl+Shift+P | Cmd+Shift+P
  2. Type Java: Configure Java Runtime and select it
  3. Verify your JDK appears in the dropdown list and is selected
  4. Select the appropriate JDK if you have multiple versions installed

VS Code Java: Configure Java Runtime

Configuring project settings

Manual Configuration

To view and modify project settings:

  1. Open the Command Palette: / Ctrl+Shift+P | Cmd+Shift+P
  2. Type Java: Open Project Settings and select it
  3. Configure as needed:
    • Source paths - Directories where your Java source files are located
    • Output path - Directory where compiled .class files will be stored
    • Libraries - External JAR files or libraries your project depends on
    • JDK version - The JDK version to use for compilation and runtime

VS Code Java Project Settings

Using .vscode/settings.json

You can also configure project settings by creating a .vscode/settings.json file in the root directory:

{
    "java.project.sourcePaths": [
        "src"
    ],
    "java.project.outputPath": "bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
}

Configuring Git

If you are using Git to version control the project, you may want to add the following to your .gitignore file:

# Compiled class files
*.class

# VS Code workspace settings
.vscode/

Running your project

  1. Open your Java file with a main method
  2. To run the code, click the "Run" button that appears above the main method or in the top right corner of the editor
  3. Check the terminal output to see your program's output

Contributors: John Wong (@Johnwz123)