Managing Java Projects in VS Code

The Project Manager for Java extension helps you to manage your Java projects and their dependencies. It also helps you to create new Java projects, packages, and classes. To get the complete Java language support in Visual Studio Code, you can install the Extension Pack for Java, which includes the Project Manager for Java extension.

Install the Extension Pack for Java

For details on how to get started with the extension pack, you can review the Getting Started with Java tutorial.

Projects view

The Java Projects view helps you view your Java projects and their dependencies, and provides entry points for project management tasks.

project view

By default, the Java Projects view is displayed below the Explorer view. If you cannot see it, try clicking the ... button in the EXPLORER title bar and select Java Projects.

In the overflow button of the navigation bar, there are more options to use. For example, you can switch between hierarchical view and flat view.

overflow button

You can find buttons next to the nodes in the Explorer, which provide useful shortcuts for some actions.

inline button

There are also many useful features in each node's context menu, you can right-click on the node in Explorer to toggle it out.

context menu

Create a new Java project

You can create a new Java project by clicking the + button in the navigation bar, or through the command: Java: Create Java Project... in Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). During creation, VS Code will facilitate installing required extension(s) per your project type, if the extension(s) weren't already installed.

create project

Import Java projects

You can directly import existing Java projects and modules to your workspace through File > Open Folder... (Make sure the opened folder contains your build tool scripts, for example, pom.xml or build.gradle). VS Code for Java will detect your projects and import them automatically.

When you add a new module into your projects, you can trigger the command Java: Import Java projects in workspace to import them to your workspace. This command helps to import new projects into the workspace without the need to reload the VS Code window.

Export to JAR

You can export your build to JAR from the projects view or by running the command Java: Export Jar....

Export jar from the project view

Configure Runtime for Projects

As Java evolves, it's common that developers work with multiple versions of JDK. You can map them to your local installation paths via the setting: java.configuration.runtimes. The setting has following format:

"java.configuration.runtimes": [
  {
    "name": "JavaSE-1.8",
    "path": "/usr/local/jdk1.8.0_201"
  },
  {
    "name": "JavaSE-11",
    "path": "/usr/local/jdk-11.0.3",
    "sources" : "/usr/local/jdk-11.0.3/lib/src.zip",
    "javadoc" : "https://docs.oracle.com/en/java/javase/11/docs/api",
    "default":  true
   },
   {
    "name": "JavaSE-12",
    "path": "/usr/local/jdk-12.0.2"
   },
   {
    "name": "JavaSE-13",
    "path": "/usr/local/jdk-13"
   }
]

Note: You can set one of them to default by adding "default": true to the entry. The default JDK will be used for your unmanaged folders (with out build tools).

To see which JDKs are used for your projects, you can trigger the command Java: Configure Java Runtime in Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). This command opens a view displaying the runtime information for your projects:

Project runtime

Change JDK for Maven and Gradle projects

If you want to change the JDK version for your Maven or Gradle projects, you need to update it in your build scripts (pom.xml or build.gradle). You can click to see how to make such changes. Click will navigate to the build script file of the project.

Change JDK for unmanaged folders

To change the JDK for unmanaged folders (with out any build tools), you can click the button. It will list all the JDKs and you can select one for your unmanaged folders.

Download JDKs

If you want to download a new JDK, you can click the download link, or trigger the command Java: Install New JDK in Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). It will open a new view guiding you to download JDKs.

Download-jdk

Note: To enable Java preview features, see How can I use VS Code with new Java versions.

Configure classpath for unmanaged folders

The Project Management for Java extension provides a user interface to configure the classpath for unmanaged folders. The classpath can be set manually in the classpath configuration page. You can open it by executing the Java: Configure Classpath command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

configure classpath

In some rare cases, you may need to clean the Java workspace by executing the Java: Clean Java Language Server Workspace command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) to let the language server rebuild your dependencies.

Dependency management

Add a Maven dependency

For Maven project, you can add a dependency by clicking the + icon next to Maven Dependencies node in project view.