Guides for SE student projects »

Caution: This page is a work-in-progress, containing content contributed by students.

VS Code - Community Resources

This guide is for those who prefer to use Visual Studio Code (VS Code) for their Java projects.

Preparing VS Code for Java projects

  1. Install the JDK needed for your project, as given here.
    IMPORTANT: If you are on a Mac and you anticipate using JavaFX in your projects, you are using the precise JDK specified here.
  2. Install the Extension Pack for Java extension to enable support for Java extensions, if you haven't done so already.
    i.e., Go to Extensions → Search Extension Pack for Java → Install.

Importing a Gradle project

  • Verify prerequisites:
    • Ensure you followed the section Preparing VS Code for Java projects above.
    • Verify the project has support for Gradle. If it does, you will see a build.gradle file in your project root.
  • Open the project in VS Code, as follows:
    • Open VS Code.
    • If you are in the welcome screen, Click Open. Otherwise, click File -> Open.
    • Select the project directory, and click Open.
  • Confirm the correct JDK is set to the one you are supposed to use for the project, as follows:
    • Open the Command Palette: / Ctrl+Shift+P | Cmd+Shift+P
    • Type Java: Configure Java Runtime and select it.
    • Check that the JDK version matches the one required for your project. If not, you can install the required JDK and configure it. (Java installation guides: Windows | Mac)
  • Confirm the correct JVM is used for Gradle, as follows:
    • Install the Gradle Extension: Search for Gradle in the Extensions view (Ctrl+Shift+X or Cmd+Shift+X), and install a Gradle extension if you haven’t already (e.g., Gradle for Java).
    • Open settings.json: You can find it by opening the Command Palette (Ctrl+Shift+P or Cmd+Shift+P), then typing Preferences: Open User Settings (JSON).
    • Add or modify the following settings to configure the JVM for Gradle. Here's an example (for Mac):
      settings.json
      {
        "java.configuration.runtimes": [
          {
            "name": "JavaSE-17",
            "path": "/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home"
          }
        ],
        "java.import.gradle.java.home": "/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home"
      }
      
    • If the Gradle icon doesn't appear after restarting VS Code, add "gradle.nestedProjects": true to your settings.json file.
  • Confirm you can access the Gradle tool window. After the importing of the project is complete (which could take a few minutes), you will see the Gradle Tab in the VSCode interface e.g., look for the elephant icon on the left and click it.

Setting up Checkstyle

Given below are the steps to set up a Checkstyle plugin in VS Code so that VS Code can alert you about code style problems as you write code.

  • Verify prerequisites:
    • Ensure you followed the section Preparing VS Code for Java projects above.
    • The two Checkstyle config files (checkstyle.xml and suppressions.xml) should be in the ./config/checkstyle directory, as mentioned here.
  • Open VS Code.
  • Download the Checkstyle for Java extension by author ShengChen using Extensions -> Search Checkstyle for Java -> Install (the first entry).
    install checkstyle extension in VS Code
  • Open the project directory in VS Code using File -> Open Folder....
  • Set up the extension to use the project's Checkstyle files as follows:
    • Check if the ./.vscode/settings.json file exists. If not, create a folder .vscode in the project root directory and add a file settings.json within the .vscode folder. This file contains configuration settings for projects in VS Code.
    • Add these configuration settings to the settings.json file. These settings ensure that the Checkstyle extension uses the correct configuration files in the ./config/checkstyle directory:
      settings.json
       {
         "java.checkstyle.configuration": "${workspaceFolder}/config/checkstyle/checkstyle.xml",
         "java.checkstyle.properties": {
           "config_loc": "${workspaceFolder}/config/checkstyle"
         }
       }    
      
  • Add .vscode to your .gitignore if you haven't done so by adding these few lines to the end of .gitignore:
    .gitignore
    # VS Code
    /.vscode/
    
  • Now you should be able to edit your code with Checkstyle violations being detected as you edit them in the Problems tab in VS Code. For example, after changing the code to add a wildcard import, you can see that the wildcard import on line 11 has been detected by the Checkstyle extension: