VS Code does not have built-in support for Java projects. You need to install specific Java extensions and configure them to specify the correct Java JDK version. Depending on the extension, you can specify the version by either setting the JAVA_HOME environment variable or by setting the java.home setting:

After installing the JDK, you would need to configure your environment for Java development. The most common way is to set JAVA_HOME environment variable to the install location of the JDK while you can also use java.home setting in Visual Studio Code settings (workspace or user settings) to configure it just for the editor.

I described the installation/setup steps in more detail below. It is mostly based on the Visual Studio Code tutorial for Java. Since the OP commented that they are using macOS, this answer is focused on the Java extension for VS Code on Mac. Also, note that depending on the version of the JDK you have installed, java.home might already be deprecated or has been completely removed, and java.jdt.ls.java.home should be used instead (source). So, replace accordingly. Lekia's comment provides a tip:

you can find java properties including java.home from terminal java -XshowSettings:properties -version


MacOS

First, you need to install the Microsoft Java Extension Pack.

Upon installation, it will display the Java Overview tab, and automatically check for available Java SDKs. If it cannot find one, it will prompt you to download one. (The Java Overview and this JDK Required page seems to also auto-appear when you open/create a .java file).

From the VS Code documentation, these JDK distributions should be supported: OpenJDK, Java SE from Oracle, Azul Zulu Enterprise. For this answer, I am using OpenJDK 11. Download and install the appropriate JDK.

Next, get the path to your JDK. Open a Terminal and then:

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

If you already have a JAVA_HOME env var set, I recommend not relying on it. I find that it is better to set the relevant java.* properties instead to configure Java specifically for VS Code (or for a particular workspace).

Now, open the VS Code Settings tab and look for the Java: Home setting:

You can modify either the User or Workspace setting. In the screenshot above, I am modifying my User settings, making the java.home/java.jdt.ls.java.home setting affect all Java projects. You can also modify the Workspace setting to configure java.home differently for each workspace.

Click on the Edit in settings.json, and add java.home (you'll know your extension was installed properly if java.* autocompletion is displayed).

Specify the path to the JDK you got earlier:

"java.home": "/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home",

Then restart VS Code. When the Java Overview tab displays again, if you set the path correctly, it should not prompt you anymore to download a JDK, meaning it found your JDK. Try running a very basic HelloWorld.java application.

Windows

If you are on Windows, they provide a special installer to help you get started:

To help you get started quickly, we created a special Installer of Visual Studio Code for Java developers.

Download Visual Studio Code Java Pack Installer

Note: The installer is currently only available for Windows. For other OS, please install those components (JDK, VS Code and Java extensions) individually. We're working on the macOS version, please stay tuned.

Answer from Gino Mempin on Stack Overflow
Top answer
1 of 9
32

VS Code does not have built-in support for Java projects. You need to install specific Java extensions and configure them to specify the correct Java JDK version. Depending on the extension, you can specify the version by either setting the JAVA_HOME environment variable or by setting the java.home setting:

After installing the JDK, you would need to configure your environment for Java development. The most common way is to set JAVA_HOME environment variable to the install location of the JDK while you can also use java.home setting in Visual Studio Code settings (workspace or user settings) to configure it just for the editor.

I described the installation/setup steps in more detail below. It is mostly based on the Visual Studio Code tutorial for Java. Since the OP commented that they are using macOS, this answer is focused on the Java extension for VS Code on Mac. Also, note that depending on the version of the JDK you have installed, java.home might already be deprecated or has been completely removed, and java.jdt.ls.java.home should be used instead (source). So, replace accordingly. Lekia's comment provides a tip:

you can find java properties including java.home from terminal java -XshowSettings:properties -version


MacOS

First, you need to install the Microsoft Java Extension Pack.

Upon installation, it will display the Java Overview tab, and automatically check for available Java SDKs. If it cannot find one, it will prompt you to download one. (The Java Overview and this JDK Required page seems to also auto-appear when you open/create a .java file).

From the VS Code documentation, these JDK distributions should be supported: OpenJDK, Java SE from Oracle, Azul Zulu Enterprise. For this answer, I am using OpenJDK 11. Download and install the appropriate JDK.

Next, get the path to your JDK. Open a Terminal and then:

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

If you already have a JAVA_HOME env var set, I recommend not relying on it. I find that it is better to set the relevant java.* properties instead to configure Java specifically for VS Code (or for a particular workspace).

Now, open the VS Code Settings tab and look for the Java: Home setting:

You can modify either the User or Workspace setting. In the screenshot above, I am modifying my User settings, making the java.home/java.jdt.ls.java.home setting affect all Java projects. You can also modify the Workspace setting to configure java.home differently for each workspace.

Click on the Edit in settings.json, and add java.home (you'll know your extension was installed properly if java.* autocompletion is displayed).

Specify the path to the JDK you got earlier:

"java.home": "/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home",

Then restart VS Code. When the Java Overview tab displays again, if you set the path correctly, it should not prompt you anymore to download a JDK, meaning it found your JDK. Try running a very basic HelloWorld.java application.

Windows

If you are on Windows, they provide a special installer to help you get started:

To help you get started quickly, we created a special Installer of Visual Studio Code for Java developers.

Download Visual Studio Code Java Pack Installer

Note: The installer is currently only available for Windows. For other OS, please install those components (JDK, VS Code and Java extensions) individually. We're working on the macOS version, please stay tuned.

2 of 9
8

Add to .vscode/settings.json:

{
    "java.jdt.ls.java.home": "<location-of-your-preferred-jdk>"
}

Note: This is only for the current project.

🌐
Visual Studio Code
code.visualstudio.com › docs › java › java-project
Managing Java Projects in VS Code
November 3, 2021 - 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: 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).
Discussions

Setting a different jdk version in settings.json (workspace)
Java version in my environmental variables is set to C:\\Program Files\\Java\\jdk1.8.0_121. As per the instruction posted here: https://github.com/redhat-developer/vscode-java#setting-the-jdk, vscode will use the workspace settings.json. More on github.com
🌐 github.com
2
May 4, 2020
can not set different jdk versions for different folder projects in the same workspace
[In vscode, how to set different jdk versions for different folder projects in the same workspace]see this issue Environment Operating System: macOS 12.4 (21F79) JDK version: 8 or 17 Visual Studio Code version: 1.68.1 (Universal) Java ex... More on github.com
🌐 github.com
1
July 6, 2022
i program in JAVA and decided to switch to visual studio code, some very noob questions
The JDK is what you need to develop anything with java. Most importantly it contains the java compiler and the JVM. The compiler will take your java code and turn it into bytecode (this is the .class file you might have seen) and the JVM is what runs your java application (it executes the bytecode). So why do you need vscode or intellij? Well technically you don't. Aside from a text editor for you to write your java code, everything you need to develop java applications is contained within the JDK, it just isn't a great development experience to not use and ide like intellij or vscode (with its extensions). I suppose a useful comparison would be to say, do I need a hammer to build a house? Well no, you could use a block of wood to hammer in all of your nails, but a hammer is a tool designed for that problem and will make your life a thousand times easier. Except this particular hammer also has a bunch of extra features that make your now easy life even easier. Why does vscode need extensions? Because vscode doesn't know about java on its own. The extension tells vscode where it can look for the compiler and other tools to help improve your development experience. What is java 11? You can use java 11 and JDK 11 interchangeably if you want. Googling java 11 will get you the download you need. Do you need to install java? Not in the traditional sense. There isn't usually a one click installer like there are for other applications. But installing java in this case means downloading the JDK and setting up the PATH environment variable and JAVA_HOME environment variable to include your JDK download. This is so that programs like vscode know where to look for the version of java you downloaded. Lastly, your rather sarcastic take on vscode at the end is actually already a thing. https://code.visualstudio.com/docs/languages/java see the section about the "coding pack for java" it is a special build of vscode that includes everything you need for java development More on reddit.com
🌐 r/javahelp
101
31
May 25, 2021
How to config java version in Visual Studio Code? - Stack Overflow
I installed OpenJDK 18 in my Fedora 36 and installed "Extension Pack for Java" on my Visual Studio Code. In my machine, there have 3 versions of java: But when I create a maven project a... More on stackoverflow.com
🌐 stackoverflow.com
🌐
YouTube
youtube.com › visual studio code
☕️ Change your Java versions in VS Code! - YouTube
#shorts Description: Take a look at how to change your Java version in VS Code! Download the Java extension at: https://aka.ms/vscodejava
Published   May 6, 2023
Views   64K
🌐
YouTube
youtube.com › shorts › JvNh4yjNpt0
How to change your Java version in VS Code! (Java 17 ☕) - YouTube
Learn how to change your Java version in VS Code! Download the VS Code Java extension here: https://aka.ms/vscodejava
Published   May 9, 2023
🌐
YouTube
youtube.com › the code city
How to Change Java JDK Version in VSCode | JDK Version Visual Studio Code (2023) - YouTube
In this video, I'll show you how to change the jdk version in vscode. We can have multiple java jdk versions, and switch them easily in visual studio code.In...
Published   November 24, 2023
Views   11K
🌐
javaspring
javaspring.net › blog › how-do-you-change-java-version-in-vscode
Changing Java Version in Visual Studio Code — javaspring.net
Changing the Java version in VSCode is a straightforward process that can be done either through the Java Extension Pack or by manually configuring the java.home setting. By understanding the fundamental concepts and following the common and best practices, you can efficiently manage different Java versions for your projects.
🌐
GitHub
github.com › redhat-developer › vscode-java › issues › 1431
Setting a different jdk version in settings.json (workspace) · Issue #1431 · redhat-developer/vscode-java
May 4, 2020 - { "java.home": "C:\\Program Files\\Java\\jdk11.0.2", "java.configuration.runtimes": [ { "name": "JavaSE-1.8", "path": "C:\\Program Files\\Java\\jdk1.8.0_121", }, { "name": "JavaSE-11", "path": "C:\\Program Files\\Java\\jdk11.0.2", "default": true } ] }
Author   redhat-developer
Find elsewhere
🌐
GitHub
github.com › redhat-developer › vscode-java › issues › 2543
can not set different jdk versions for different folder projects in the same workspace · Issue #2543 · redhat-developer/vscode-java
July 6, 2022 - [In vscode, how to set different jdk versions for different folder projects in the same workspace]see this issue ... The java version is always automatically set to the same.
Author   redhat-developer
🌐
Microsoft
devblogs.microsoft.com › dev blogs › microsoft for java developers › about upgrading to java 11 for vs code for java
About Upgrading to Java 11 for VS Code for Java - Microsoft for Java Developers
September 7, 2020 - That means, you can continue developing your project with version 1.5 or above. Behind scene, JDKs for your project and for running VS Code for Java are specified by configurations of java.configuration.runtimes and java.home respectively. Our Configure JDK document provides more details. For a smooth and quick upgrade to Java 11 for running VS Code for Java, here are steps we suggest you follow: 1. Download and install a Java 11 JDK, if haven’t.
🌐
Reddit
reddit.com › r/javahelp › i program in java and decided to switch to visual studio code, some very noob questions
r/javahelp on Reddit: i program in JAVA and decided to switch to visual studio code, some very noob questions
May 25, 2021 -

i get totally frustrated from the confusing terms used in Java, i program at the university and find myself angry on simple questions that seems like everyone know except for me.

1 - what is JDK ?in a website like this: https://www.infoworld.com/article/3296360/what-is-the-jdk-introduction-to-the-java-development-kit.htm

'' Developers new to Java often confuse the Java Development Kit and the Java Runtime Environment. The distinction is that the JDK is a package of tools for developing Java-based software, whereas the JRE is a package of tools for running Java code. ''

JDK is meant for developing tools for java?
so why do i need intelliji or visual studio code ? and why do i need to download special extension for visual studio code in order to code Java ? after all, JDK will tend to it, right ?

2 - in visual studio it says

'' Java 11 or more recent is required to run the Java extension. ''

but what is ''java'' ? JVM ? JRE ? JDK ? something else ?

3 - lets continue reading this website:https://www.infoworld.com/article/3296360/what-is-the-jdk-introduction-to-the-java-development-kit.htm

'' Getting Java setup in your development environment is as easy as downloading a JDK and adding it to your classpath ''

and what does that mean ? what is a classpath ?

do i need to install JDK ? because i got JDK from redhat and it didn't had any installer file there.

Summary:i really don't understand why programs like visual studio code or intelliji are making it so damn hard to even start programming java.
here is an alternative reality: when i download visual studio code, it already download JDK (mind blowing, i know) , put it in correct place in the files, and set the ''classpath'' by itself. why do i need to tend to all of this stuff ?

Top answer
1 of 5
25
The JDK is what you need to develop anything with java. Most importantly it contains the java compiler and the JVM. The compiler will take your java code and turn it into bytecode (this is the .class file you might have seen) and the JVM is what runs your java application (it executes the bytecode). So why do you need vscode or intellij? Well technically you don't. Aside from a text editor for you to write your java code, everything you need to develop java applications is contained within the JDK, it just isn't a great development experience to not use and ide like intellij or vscode (with its extensions). I suppose a useful comparison would be to say, do I need a hammer to build a house? Well no, you could use a block of wood to hammer in all of your nails, but a hammer is a tool designed for that problem and will make your life a thousand times easier. Except this particular hammer also has a bunch of extra features that make your now easy life even easier. Why does vscode need extensions? Because vscode doesn't know about java on its own. The extension tells vscode where it can look for the compiler and other tools to help improve your development experience. What is java 11? You can use java 11 and JDK 11 interchangeably if you want. Googling java 11 will get you the download you need. Do you need to install java? Not in the traditional sense. There isn't usually a one click installer like there are for other applications. But installing java in this case means downloading the JDK and setting up the PATH environment variable and JAVA_HOME environment variable to include your JDK download. This is so that programs like vscode know where to look for the version of java you downloaded. Lastly, your rather sarcastic take on vscode at the end is actually already a thing. https://code.visualstudio.com/docs/languages/java see the section about the "coding pack for java" it is a special build of vscode that includes everything you need for java development
2 of 5
13
Visual studio mixed with java screams rookie programmer very loud. Don't waste your time and go with intellij (it was made for java)
Top answer
1 of 1
2

Only for 32bit START

+++Please mind that 32bit is usually outdated. This is probably just for those who have an old netbook lying around.+++

You can just switch to OpenJDK 14, which is recommended by vscode "itself" (Oracle JDK has stopped support for 32bit after version 8).

This vscode site https://github.com/redhat-developer/vscode-java/wiki/JDK-Requirements#jdk11.requirement recommends the following link right at the top at the Java Development Kit link:

https://adoptopenjdk.net/

A non-Oracle JDK is thus the only way to use java 14 on a 32bit.

Only for 32bit END

####

32bit and 64bit START

For those who cannot install JDK 11 or higher due to a 32bit system together with a not allowed OpenJDK JDK (that is, if Oracle JDK is obligatory), or those who have other restrictions on 64bit, one workaround is to install vscode-java version 0.64.1 instead of the most recent 0.65.0. (This again, is not recommended unless you have restrictions on the JDK version, it is just possible, see above)

See https://github.com/redhat-developer/vscode-java/issues/1543#issuecomment-669895753

How to install the earlier extension version:

EDIT

You should not do the following if you have no other restrictions. The most plausible restriction is that you use Windows 32bit and you have to use an Oracle JDK (Oracle JDK 8 is the last version supporting 32bit). Or if you must use Gradle with a version below 4.7, see the github discussion again.

In the usual case: upgrade, and then you can still go back to using an older java version by changing the settings according to https://github.com/redhat-developer/vscode-java/wiki/JDK-Requirements#jdk11.requirement:

Do I need to migrate my projects to Java 11?

"NO, you don't! Well, you should, be we're not here to judge. It is still possible to compile/run Java applications from Java 1.5 to 14, provided the proper java.configuration.runtimes are configured in the user's settings.json."

--> This means you can upgrade the extension and use the Java runtime of your choice. See detailed instructions for Windows 64bit at https://github.com/redhat-developer/vscode-java/issues/1543#issuecomment-671106893.

🌐
TikTok
tiktok.com › visual studio code (@vscode) official | tiktok › java 17☕change your java version in vs code!
Java 17☕Change your Java version in VS Code! | TikTok
288 Likes, TikTok video from Visual Studio Code (@vscode): “Java 17☕Change your Java version in VS Code!”. original sound - Visual Studio Code.
🌐
Visual Studio Code
code.visualstudio.com › docs › languages › java
Java in Visual Studio Code
November 3, 2021 - Note: If you have multiple JDKs installed and need to use a specific JDK version for your projects, see Configure Runtime for Projects. To enable Java preview features, see How can I use VS Code with new Java versions.
🌐
Reddit
reddit.com › r/salesforce › error while setting up visual studio
r/salesforce on Reddit: Error while setting up Visual Studio
November 26, 2023 -

Hi guys!

I'm trying to setup VS to play around with some trailhead Apex.

I get the following in VS "The salesforcedx-vscode-apex.java.home setting defined in VS Code settings points to a missing folder. For information on how to setup the Salesforce Apex extension, see Set Your Java Version."

While it is pretty self-explanatory, I can't seem to fix it. I did already follow some instructions from Stack Overflow and some other SF related forums but without any luck... Could someone share if they experience the same and if yes how did you fix it ?

Thanks!

🌐
Manjaro Linux
forum.manjaro.org › support › software & applications
To Java 20 in vscode - Software & Applications - Manjaro Linux Forum
July 13, 2023 - I trying to figur out how to change from /usr/lib/jvm/java-11-openjdk/bin/java to java-20-openjdk. In vscode. java-20-openjdk is set as (default) on my system and in settings.json I try this without success: "java.configuration.runtimes": [ { "name": "JavaSE", "path": "/usr/lib/jvm/default-runtime/bin/java", // "path": "/usr/lib/jvm/default/bin/java", "default": true } ], also I try this in my zshrc : # export JAVA_HOME=...
🌐
Loiane Groner
loiane.com › 2024 › 03 › visual-studio-code-for-java-the-complete-guide
Visual Studio Code for Java: The Complete Guide (Tips, Setup, and Extensions) | Loiane Groner
March 19, 2024 - To update the JDK for the project, open the Java Overview screen again (via Command Pallete), and select Configure Java Runtime. It will open the projects you have in your current VSCode workspace and a dropdown will be available to select from ...
🌐
Salesforce Developers
developer.salesforce.com › docs › platform › sfvscode-extensions › guide › java-setup.html
Java Setup | Installation Instructions for Desktop | Salesforce Extensions for Visual Studio Code | Salesforce Developers
Change the salesforcedx-vscode-apex.java.home setting to the full pathname of your Java Runtime. Do not set it to the Java executable itself. This pathname can’t point to a location inside your project folder. Also note that backslashes must be escaped on Windows. Some examples of folder paths to the Java 21 runtime: ... If you see an error message that your Java version is not supported, open the Output tab on the bottom panel of VS Code.
🌐
Microsoft
devblogs.microsoft.com › dev blogs › microsoft for java developers › java on visual studio code update – november 2020
Java on Visual Studio Code Update – November 2020 - Microsoft for Java Developers
December 15, 2020 - When your project uses Maven or Gradle, WorkSpace Overview lists the language version set by the build tool; Maven/Gradle Projects lists all eligible JDKs installed in your environment per the language version and also allows you to specify a JDK for your project. Java Tooling Runtime tab allows you to specify runtime to run VS Code’s Java language tools, a.k.a. the Java Language Server. The tab only lists Java 11 or above JDKs installed in your environment, as required by the language server. Please, remember to reload VS Code to make change effective.