In this article, we'll learn how to configure Visual Studio Code with Java, and how to use its basic features for this language.
Then, we'll see the Maven and Gradle integrations and conclude with the strengths and the drawbacks of this editor.
2. Visual Studio Code Setup for Java
Microsoft improved a lot the developer experience to configure their editor for Java. We can download the Coding Pack for Java, which is a set of essential extensions (the equivalent of JDT for Eclipse).
Even if we haven't installed anything yet, this executable package will check missing software and install them for us:
- Visual Studio Code
- Java Development Kit (JDK)
- Java Extension Pack, which includes:
- Language Support for Java™ by Red Hat: navigate, write, refactor, and read Java files
- Debugger for Java, by Microsoft: launch/attach, breakpoints, evaluation, show call stack
- Maven for Java, by Microsoft: generate projects from Archetype, run Maven goals
- Java Test Runner, by Microsoft: run Junit, TestNG
- Project Manager for Java, by Microsoft: show project view, create a new project, export jar
- Visual Studio IntelliCode, by Microsoft: advanced auto-completion features
If we already have Visual Studio Code installed, we just have to install the Java Extension Pack from the Extensions button in the sidebar. Now, we're able to view the Create Java Project button and the Maven view on the left:
We can also browse the Java features through the View > Command Palette menu:
Next, we'll learn how to use the features included in these extensions.
3. Working with a Basic Java Project
3.1. Create or Open a Java Project
If we want to create a new Java project, we'll find the Java: Create Java Project command in the Command Palette menu, which opens a top menu where we can pick our project type:
- No build tools creates a blank project with src and lib directories
- Maven lets us pick an archetype from a large library collection, as we'll see in a later section
- Spring Boot, Quarkus, and MicroProfile require that we install their respective extensions to create a project
If we need to open an existing project, Visual Studio Code will show a small popup at the bottom-right corner to import the folder as a Java project. If we missed it, we can open any Java source file to show it again.
3.2. Run and Debug the Project
To run the project, we just have to press F5 (debug) or Ctrl-F5 (run). We can also use the Run|Debug shortcuts just above the main methods or the unit tests:
We can also see the debug toolbar at the top of the window to stop, rerun, or continue execution.
Then, the Terminal view at the bottom will show the output logs.
3.3. Manage Java Packages and Imports
The first pain point we'll notice is that Visual Studio Code does not provide dedicated features to create a class or package.
To create the whole directory structure of our package, we must first create a Java file and declare the required package at the top. After that, Visual Studio Code will show an error: we just have to roll over it to display the Quick Fix link. This link will create the appropriate directory structure if it doesn't exist.
However, the package management works like in other Java IDE: just press Ctrl+Space and it will propose, for example, to pick an existing class and import it on-the-fly. We can also use the quick fix popup to add missing imports or to remove unused ones.
3.4. Code Navigation and Auto-Completion
The most useful shortcuts to know are Ctrl+P to open a file and Ctrl+T to open a class or interface. Similar to other Java IDEs, we can navigate to a method call or a class implementation with Ctrl+click. There is also the Outline view from the sidebar, which helps us to navigate through large files.
The auto-completion is also working like in other IDEs: we just press Ctrl+space to display options. For example, we can see the possible implementations of an interface or the available methods and attributes of a class, including their Javadoc. The auto-completion can also generate a code snippet if we press Ctrl+space after statements like while, for, if, switch, or try.
However, we can't generate Javadoc for method arguments.
3.5. Compilation Errors and Warnings
We'll see compilation errors at first with underlined code. Unused variables are grayed-out, We can also display the full list of errors and warnings from the View > Problems menu. Both propose quick fixes for the basic ones.
4. Maven and Gradle Integration
If we choose to create a new Maven project, the Command Palette provides a large collection of Maven archetypes. Once we select one, we're prompted to pick the destination folder, and then the configuration takes place in an interactive terminal, not in a graphical wizard, like in other Java IDEs.
The Java import popup will be displayed first, then the Maven configuration will start. The extensions will use the global Maven client defined in our PATH variable. However, if the Maven Wrapper is configured in our project, a popup will let us choose if the wrapper should be used instead of the global Maven client.
Then, from the Maven side-view, we'll see the Maven plugins and goals that can be launched:
If not, we need to check for errors in the following locations:
- From the View > Problems menu, which contains every problem related to the pom.xml file and JDK compatibility problems
- From the View > Output menu, select Maven For Java from the bottom-right list to show Maven client and wrapper problems
To work with Gradle, we must install the Gradle Extension Pack from the Extensions panel. This extension manages projects only if Gradle Wrapper is configured.
After opening a Gradle project, we'll see the status bar at the bottom indicating the download and installation progress. We check if any error occurred by clicking this bar. We can also display the Output view and choose Gradle Tasks option from it.
Then, we'll be able to see the Gradle elephant icon in the sidebar, which displays dedicated Gradle panels to control tasks:
If this icon is not shown, we have to check that our Gradle project is in a sub-directory. In this case, we have to enable the gradle.nestedProjects setting to discover it.
5. Strengths and Drawbacks
First, we have to admit that this lightweight editor offers fewer features than its counterparts: no wizard, Maven and Gradle integration is not very handy, and basic features are missing, such as tools to manage packages and dependencies. Visual Studio Code wasn't designed for Java, and that's something we can easily notice, especially if we're familiar with other Java IDEs.
However, core capabilities such as error detection and auto-completion are very complete, as they're using the Eclipse JDT language server. Moreover, the popularity of Visual Studio Code comes from its fast launch speed, its limited use of resources, and its better user experience.
In this article, we learned how to configure Visual Studio Code for Java, its supported features for this language, and saw its strengths and drawbacks.
In conclusion, if we're already familiar with Visual Studio Code, it can be a good editor to start learning Java. But if we're already advanced Java IDE users and satisfied to work with them, we might be disappointed to lose some of the comforts we've taken for granted.