Java Project Management in VS Code
If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.
Project Management in Visual Studio Code is provided by the Java Dependency Viewer extension. This extension has many features including creating projects as well as viewing the package structure of the project and its dependencies.
In addition to creating a project through Maven Archetype, you can also use the following command to create a simple Java project: Java: Create Java Project from the Command Palette.
Package and dependency view
The extension also has a hierarchy view of your project and dependencies, which supplements the file view and outline provided by Visual Studio Code, so you don't need to expand multiple subfolders to just view your Java package.
Standalone Java file support
Visual Studio Code also supports Java files without a project (or an invisible project, which it creates without affecting your folders). The solution is folder-based, so you can open a folder with Visual Studio Code, and all the Java files within the folder will be properly compiled with all language features available. You can also run and debug standalone files.
Multiple source folders
If you have multiple subfolders that have source code for your project, you can easily add these folders to source path, then all the code inside will be correctly compiled.
Adding external JAR
In case you need to bring additional JAR files as dependencies, you can create a
lib/ folder in the root directory of the standalone files and place your JAR files there. Source for
foo.jar is automatically detected if there is a
foo-sources.jar in the
In case VS Code throws an error for a classpath issue, try setting your classpath manually by either setting the CLASSPATH environment variable or editing the
.classpath file with the path to the JAR file:
<classpathentry kind="lib" path="lib/log4j.jar"/>
In some rare cases, you may need to clean the Java workspace by executing the Java: Clean the java language server workspace command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) to let the language server rebuild your dependencies.
Maven is a software tool that helps you manage Java projects and automate application builds. The Maven for Java extension for Visual Studio Code provides fully integrated Maven support, allowing you to explore Maven projects, execute Maven commands, and perform the goals of build lifecycle and plugins.
Exploring Maven project
Once a Maven project is loaded, the extension will be activated and it will automatically scan for
pom.xml files in your workspace and displays all Maven projects and their modules in the side bar.
Resolve unknown type
The Maven extension also supports searching Maven Central to resolve unknown types in your source code. You can do this by selecting the Resolve unknown type link shown on hover.
Working with POM.xml
The extension provides code snippets and auto completion for adding Maven dependencies based on local Maven repositories. See how easy it is to add a new dependency to your
pom.xml with those convenient features.
The extension also enables you to generate effective POM.
You can also use the command Maven: Add a Dependency (or
maven.project.addDependency) to help add a new dependency to
pom.xml. The process is interactive.
Furthermore, VS Code also supports showing dependencies in a tree view, which allows you to inspect all dependencies in your project at a single place and check for potential issues.
Execute Maven commands and goals
By right-clicking each Maven project in the explorer, you can conveniently run Maven goals.
The extension also preserves the history of goals for each project, so you can quickly rerun the previous command, which is useful when you're running a long custom goal.
There are two ways to rerun a goal:
- Command Palette > Select Maven: History > Select a project > Select command from the history.
- Right-click a project > Click History > Select command from history.
You can also specify your favorite commands in settings for future execution.
For each plug-in you use with your project, the extension also provides you an easy way to access the goals within each plugin.
To debug Maven goals, right-click on a goal and start debugging. The Maven extension will call the Java debugger with the right parameters. This is a handy, time-saving feature.
Generate project from Maven Archetype
Another handy feature provided by this extension is to generate a Maven project from Archetype. The extension loads archetypes listed in local/remote catalogs. After selection, the extension sends
mvn archetype:generate -D... to the terminal.
There are two ways to generate a Maven project:
- From the Command Palette, select Maven: Generate from Maven Archetype.
- Right-click on a folder and select Generate from Maven Archetype.
When you edit Java source code in Visual Studio Code, the Java language server is building your workspace to provide you with the necessary language features. You can see the detailed build task status and watch what is happening behind the scene by clicking the language server Status bar icon in the lower right.
In addition to Maven, there's also a Bazel extension if you use Bazel to build and test your project.
Read on to find out more about: