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_HOMEenvironment variable to the install location of the JDK while you can also usejava.homesetting 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.homefrom terminaljava -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:
Answer from Gino Mempin on Stack OverflowTo 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.
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_HOMEenvironment variable to the install location of the JDK while you can also usejava.homesetting 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.homefrom terminaljava -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.
Add to .vscode/settings.json:
{
"java.jdt.ls.java.home": "<location-of-your-preferred-jdk>"
}
Note: This is only for the current project.
Setting a different jdk version in settings.json (workspace)
can not set different jdk versions for different folder projects in the same workspace
i program in JAVA and decided to switch to visual studio code, some very noob questions
How to config java version in Visual Studio Code? - Stack Overflow
Videos
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 ?
To config to the right JDK in VS Code. Open File -->Settings --> Java --> Configuration:Runtimes -->[Edit in Setting.json] add:
"java.configuration.runtimes": [
{
"name": "JavaSE-18",
"path": "/usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64",
"sources" : "/usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64/lib/src.zip",
"javadoc" : "/usr/share/javadoc/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64/api",
"default": true
}
]
The path is the "$JAVA_HOME" if you run
$sudo alternatives --config java and the result is
+ 3 java-latest-openjdk.x86_64 (/usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64/bin/java)
then JAVA_HOME is /usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64
The source, if you install sudo dnf install java-latest-openjdk-src.x86_64 the source code will be $JAVA_HOME/lib/src.zip
To find the Javadoc if you install "java-latest-openjdk-javadoc.x86_64",
you can use command rpm -pl java-latest-openjdk-javadoc.x86_64 to check the installation location.
Use Ctrl+Shift+P to open the command palette, search for and select Java: Configure Java Runtime .

or click the three dots after the Java project and select Configure Java Runtime.

Select the version you want from the drop-down options.

On Mac and VSCode Version: 1.86.2 I was unable to select an alternative JDK for my Java project. In particular I wanted to run my Java classes with a specific JDK.
I tried to add the JDK to the settings.json file ( with both a "java.jdt.ls.java.home" entry and a entry with "java.configuration.runtimes" but VSCode always run the wrong JDK. Even if setting proper JAVA_HOME before starting VSCode did not help.
Eventually I modified the launch.json file by adding a javaexec entry and then I could control what JDK to use! Below I pinpoint my JDK 21 installed using homebrew;
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "MyJavaClass",
"javaExec": "/usr/local/Cellar/openjdk/21.0.1/libexec/openjdk.jdk/Contents/Home/bin/java",
"request": "launch",
"mainClass": "se.myorg.MyJavaClass",
"projectName": "myproject"
}]
}
If you follow this link you should find some helpful information. VSC supports Java versions 1.5 and above
If I could give my two cents however, there are many IDEs that are much more intuitive and have built-in Java support where VSC does not. IDEs like IntelliJ or Eclipse are your best options for debugging, maven, library support, etc. (IntelliJ is my personal favourite).
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!
Hi,
I need help with VS Code IDE.
There is a project that I have imported and it is using 1.8 Java JDK.
But, VS Code just asked me to download JRE 11, which I did, thinking the project is running on JRE 11, which is not after I checked.
Now, I have problem in reversing back to JDK 1.8.
Hope someone can advise me how to.
Tk.s
