How to Configure VS Code for Java in 2022
Published: 2022-07-18 13:29:28

I recently shifted from IntelliJ IDEA Ultimate to Visual Studio Code (VS Code) and I have no regrets! VS Code is highly customizable and fast. It stores your settings in the cloud so you don’t have to worry about configurations when you move to a different machine.

As expected, it has support for Java, Git, SQL, Spring Boot, and many other languages, frameworks, and tools.

In this article, I’ll show you the extensions and configurations that I use with my new favorite IDE.

## Java extension pack

VS Code has tons of extensions that add support for programming languages, frameworks, and tools. An extension pack groups related extensions that work well together. So the first thing to do is install the [Java Extension Pack](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack):


![Java Extension Pack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfvsua5jzbqhy7lq1su2.jpg)

## Spring Boot, Microprofile, and Quarkus extensions

There are extensions for [Spring Boot](https://marketplace.visualstudio.com/items?itemName=pivotal.vscode-boot-dev-pack), [Microprofile](https://marketplace.visualstudio.com/items?itemName=MicroProfile-Community.vscode-microprofile-pack), and [Quarkus](https://marketplace.visualstudio.com/items?itemName=redhat.vscode-quarkus) if you are using any of these frameworks. These extensions make it easy to create new projects, deploy and run applications, as well as configure them.

## Creating a new Java project

To create a new Java project, in the **Explorer** tab on the sidebar, click **Create Java Project**:


![Create Java Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo6t8fde4z6ck6ez1lr9.jpg)

You can select from a variety of frameworks depending on the extensions you installed. Here are some of the ones that I see in my IDE:

![Creating a new Java project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hijfkmj9zx5d4hd5olps.jpg)

  
Each option will prompt further information to generate the new project.

## Importing a project

The Java Extension Pack includes the [Maven for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven) extension (there’s a [Gradle extension](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-gradle) as well). VS Code recognizes Maven projects when you open a directory with a **pom.xml** file in it. Simply go to **File | Open…** in the menu and select the directory that contains the **pom.xml** file.

## Code command

Although you don’t have to, you can also use the command line to open projects. VS Code makes it extremely easy to enable this. Press **F1** and type **shell** to include the `code` command in your `PATH` environment variable:


![Code command installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttcpu0bygkpju6k458bu.jpg)

After this, make sure to restart the terminal (if you have any opened), and type **code** to launch VS Code. You can specify a directory to open:

To open the current directory, specify a dot:


`code .`

## Running an application

Once you have a new or an existing project opened in VS Code, you can run it by pressing **F5**. Select **Java** and the class that contains the standard Java entry point `main` method that you want to run:

![Running an application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ppgjayj9q7ux52ey30l.jpg)  

## Database client

Most applications connect to databases like MariaDB, MySQL, PostgreSQL, MongoDB, etc. The best extension for this is [Database Client](https://marketplace.visualstudio.com/items?itemName=cweijan.vscode-database-client2). You can try it out with [SkySQL](https://cloud.mariadb.com/) for example. Make sure to enable the **Use SSL** option, download the CA file from SkySQL, and specify the path to the file in the **Client Cert** field in VS Code. After that, you can run SQL queries without having to leave your IDE. Check this video:

[![SQL client for Visual Studio Code](https://img.youtube.com/vi/rJYUTxD-2-M/0.jpg)](https://www.youtube.com/watch?v=rJYUTxD-2-M)

## Git

VS Code includes Git support out-of-the-box. However, I suggest installing the [GitLens](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens) extension to add useful views to the **Source Control** sidebar panel:

![Git support](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kex4i6juaccn3fjtfr1s.jpg)

In my case, I disabled the current line blame annotations since I find it a bit intrusive, but try it out and decide for yourself.

## Keyboard shortcuts

If there’s one shortcut you should learn, that is the sequence **Ctrl K**, **Ctrl S** (or **Cmd K**, **Cmd S**). It shows all the keyboard shortcuts with the option to search and edit the key combinations as you wish:

![Keyboard shortcuts](https://cdn.hackernoon.com/images/-cld341m.gif.webp)

Here are a few keyboard shortcuts that I use frequently:

**Ctrl B** / **Cmd B** - Toggle sidebar

**Ctrl Shift E** / **Cmd Shift E** - Show/focus explorer

**Ctrl J** / **Cmd J** - Toggle panel (terminal, output, debug console, etc.)

**Ctrl \`** / **Ctrl Shift \`** - Toggle terminal

**F5** - Run the application

**Ctrl ;** / **Cmd ;** - Settings

**Ctrl Shift P** / **Cmd Shift P** / **F1** - Show all available commands

**Ctrl P** / **Cmd P** - Open a project file

**Ctrl Shift O** / **Cmd Shift O** - Outline (to show for example class members)

## Presentation Mode

If you are presenting your code to an audience, it’s always useful to increase the font size of the code. Most IDEs allow you to do this, but the size of all other things in the UI remains the same, which is not ideal in my opinion. VS Code is the best at this since you can simply hit **Ctrl +** (or **Cmd +** on macOS) and the whole UI is zoomed in, just like in a web browser. Use **Ctrl -** (or **Cmd -** on macOS) to zoom out.

You might want to go full screen (**F1** and search for **Toggle Full Screen**) or even better activate the _Zen mode_ (**F1** and **Toggle Zen Mode**). I do this in presentations and videos like this:

[![ASMR Programming: Java 18 Simple Web Server – No Talking](https://img.youtube.com/vi/cHGQxMkpCMM/0.jpg)](https://www.youtube.com/watch?v=cHGQxMkpCMM)

#Other resources

Like almost always the official [documentation](https://code.visualstudio.com/docs) is the best source of knowledge. It includes resources for multiple programming languages like JavaScript, TypeScript, Java, Python, and C++. Check the [official YouTube channel](https://www.youtube.com/c/Code) as well and visit the [GitHub repository](https://github.com/microsoft/vscode). VS is open source!