One of the most common stumbling blocks when you start learning a new programming language is picking the right IDE or code editor.
That gets worse when you're dealing with Java. You're usually staring at a huge pile of legendary IDE (Integrated Development Environment) options like NetBeans, IntelliJ IDEA, Eclipse, and their friends.
The trouble is that in many cases these giant IDEs make your laptop struggle — especially when the specs are just barely enough, the classic potato spec. 😂 The cause is the number of heavy processes running in the background, and you don't necessarily need any of them to write simple code.
The alternative you can take is switching to a code editor. Compared with an IDE, a code editor tends to be much kinder to your laptop's RAM and CPU. Its main job is focused on editing code alone, without an overloaded set of built-in features.
Of the many options out there, one of the most popular and best loved is VS Code. It's not just for Java either; this versatile editor works for any programming language, as long as you know how to configure it properly.
So this time we'll go through how to install and configure VS Code so you can use it for Java coding smoothly and happily.
Key takeaways
- Visual Studio Code (VS Code) is a far lighter code editor than traditional Java IDEs like NetBeans or Eclipse.
- The JDK (Java Development Kit) is the mandatory component for compiling and running Java programs on your machine.
- Microsoft ships an OpenJDK distribution as an installer (.msi) that is very easy to put on a Windows system.
- With the 'Extension Pack for Java' installed in VS Code, you get autocomplete and one-click code execution.
At a Glance
Before you start the installation, make sure your laptop or PC is connected to the internet. That's because you'll be downloading a few important pieces of software below:
- Java Development Kit (JDK) — The main engine for running Java.
- Visual Studio Code — Your trusty code editor.
- Java extension for VS Code — A support package that makes VS Code smarter at reading Java code.
Once everything is installed correctly, your work environment will be complete with a file explorer panel, a code editor, and a tidy execution console.
📷 Illustration: the VS Code interface with a Java project open, showing the source code and the terminal with the program's output.
Let's start with step one and install the JDK first.
1. Installing the Java Development Kit (JDK)
The JDK is the main piece of software you must have to build and run Java applications. The JDK package already includes the API, loader, compiler, runtime environment (JRE), and many other important components.
For the JDK installation, adjust the steps to the operating system you're using right now.
Installing the JDK on Windows
Windows users can use the official JDK distribution that Microsoft provides directly.
The installation is easy. Just download the installer file and run it following the prompts, like any other Windows application.
You can reach the download page on the official Microsoft OpenJDK site. Make sure you download the latest version in the .msi installer format. Don't forget to match the architecture of your machine too, which is usually 64-bit.
Note: When this article was written, the recommended stable version was
17.0.4.1(LTS).
Once the file finishes downloading, open that .msi file and follow the installation steps. We'll use the standard (default) configuration here, so just click Next, Next, until Finish.
When the installation finishes, let's confirm the JDK is registered properly on the system. Open your terminal app (CMD or PowerShell is fine). Type the command below and press enter:
java --version
If the command prints OpenJDK version information like the output below, your JDK setup is installed successfully:
openjdk 17.0.4.1 2022-08-12 LTS
OpenJDK Runtime Environment Microsoft-40354 (build 17.0.4.1+1-LTS)
OpenJDK 64-Bit Server VM Microsoft-40354 (build 17.0.4.1+1-LTS, mixed mode, sharing)
But if the command isn't recognized, try restarting your computer first, then run the check command again.
If that's safe, let's move to the next step, installing VS Code.
Installing the JDK on Linux
For Linux users, some distributions (distros) already ship the JDK by default. You can confirm it by typing this command in the terminal:
java --version
If information shows up, you can jump straight to the VS Code installation step.
If the result says the command isn't found, you'll have to install it manually first.
Fortunately, almost every popular Linux distro already provides OpenJDK packages in its official repositories. So you just run the installation command in the terminal for the family of distro you use.
For the Debian, Ubuntu, Mint, and derivative family:
sudo apt update
sudo apt install default-jdk
For Fedora users:
sudo dnf install java-11-openjdk
For Arch Linux users and its derivatives (pacman):
sudo pacman -S jdk-openjdk
For other Linux distros, just adjust the command to each one's package manager.
2. Installing Visual Studio Code
To install VS Code, download the installer from the official VS Code download page. The installation steps on Windows and Linux are pretty similar and very easy to follow. Just make sure the file format you download matches your operating system, such as .exe for Windows or .deb/.rpm for Linux.
Once the installation is done, launch the app until the welcome tab (Welcome page) appears. This first screen gives you quick shortcuts to open a work folder, recent files, and basic configuration menus.
📷 Illustration: the Visual Studio Code start window showing the welcome tab and the activity bar on the side.
Nice, one crucial step left! 👏
3. Installing the Java Support Extension
At this point you can actually already write and compile Java code from the terminal. But honestly, that manual route feels tedious, because you have to keep typing compile commands back and forth.
Fortunately, VS Code has a very rich extension ecosystem that makes your life easier. You just install an extension bundle called Extension Pack for Java, which packs a set of helper tools for writing, testing, and debugging Java programs.
Installing it is easy: click the Extensions icon in the navigation menu (sidebar) on the left.
Then type the keyword "Java" in the search box. In the top results you'll see the Extension Pack for Java bundle from Microsoft. Click Install and wait a moment for the installation to finish.
📷 Illustration: the VS Code extensions panel showing the Extension Pack for Java bundle with its install button.
Now that the extension is ready, let's try creating your first Java project with VS Code.
4. Creating Your First Java Project
The first step for a new project is calling up the command palette feature. Press the shortcut combination Ctrl + Shift + P on your keyboard, and a command input box appears at the top of the screen.
Type the keyword Java: Create Java Project in that box, then press enter or click the option that appears on screen.
📷 Illustration: the Command Palette bar at the top of VS Code filtered to the Java project creation command.
Next, you'll be asked to choose the type of project to create. Since you're still at the basic learning stage, choose No build tools to build the smallest possible project structure without any external dependency manager. This menu also offers other tool choices such as Maven or Gradle for more complicated project needs.
📷 Illustration: the list of Java project build tool options in the VS Code dropdown menu.
After that, a save dialog (pop-up) appears asking you to pick a project folder location. Choose any folder on your computer that fits your needs.
Once the location is set, a small input field (text field) appears for typing your project name. Type any name you like (we'll call it demo), then press enter.
VS Code processes your project directory structure automatically and opens it in a new workspace. In the side panel, the project files are laid out neatly, from the main folder down to the source code subfolder.
📷 Illustration: the VS Code file explorer panel showing the Java project directory structure with the src and lib folders.
As a basic rule, every Java code file you write later has to live inside the src folder. There you'll also see a built-in file named App.java containing a simple text-display program.
To run the code, just click the triangle icon button (Run) in the top right corner of the editor. Or, as a nice alternative, click the floating Run button text that sits neatly right above the main function declaration.
When the code executes, the terminal panel at the bottom of the screen opens automatically and shows your program's output.
Hooray! 🥳 At this point the whole Java configuration using VS Code is done. Now you're ready to start your adventure writing lines of Java code comfortably.
Have fun and happy coding! 👋