Sub topic 4
Installing a Java JDK and pointing the extensions at it
This chapter assumes you have VS Code installed, can run commands in a terminal, and have installed the Salesforce CLI in the previous chapter. You will install a Java JDK, confirm the shell can find it, and tell the Apex extension which JDK to use.
Why Apex needs Java
The Apex language server is a separate program that VS Code starts in the background. It provides code completion, go-to-definition, and diagnostics. It is written in Java, so it needs a Java runtime on your machine. A JDK (Java Development Kit) includes the runtime, so installing a JDK covers the requirement.
If the extension cannot find Java, the editor still opens .cls files, but you get no completion, no outline, and no error squiggles.
Choose a JDK version
Salesforce documents the supported Java versions for the extensions, and the list changes over time. At the time of writing, Java 11 and Java 17 are supported, with newer long-term-support releases added as the extensions are updated. Check the current list in the Salesforce Extensions documentation before you choose.
Any vendor's build works. Common choices are Eclipse Temurin, Amazon Corretto, and Oracle JDK. Install a JDK, not only a JRE (Java Runtime Environment), because some distributions ship the two separately.
Install the JDK
Use the installer or package manager for your operating system.
- macOS with Homebrew:
brew install --cask temurin@17 - Windows with winget:
winget install EclipseAdoptium.Temurin.17.JDK - Linux (Debian or Ubuntu):
sudo apt install openjdk-17-jdk
You can also download an installer from the vendor's site. On Windows, choose the option that sets JAVA_HOME and adds Java to the PATH if the installer offers it.
Verify the installation
Open a new terminal, because existing terminals do not see environment changes. Then run the checks from the tooling overview:
code --version
sf --version
java -versionjava -version should print a version such as 17.0.x. If it prints "command not found", the JDK is not on your PATH.
Point the extension at the JDK
In most cases the extension finds Java without help. When it does not, or when you have several JDKs, set the path explicitly.
- Open the Command Palette and run Preferences: Open User Settings (JSON).
- Add the
salesforcedx-vscode-apex.java.homeentry. - Save the file and reload the window with Developer: Reload Window.
{
"salesforcedx-vscode-apex.java.home": "/path/to/your/jdk"
}The value must be the JDK's home directory, not the java executable inside it. The home directory is the one that contains the bin and lib folders. Typical locations are:
| System | Example path |
|---|---|
| macOS | `/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home` |
| Windows | `C:\\Program Files\\Eclipse Adoptium\\jdk-17.0.x-hotspot` |
| Linux | `/usr/lib/jvm/java-17-openjdk-amd64` |
In JSON, Windows backslashes must be doubled, as shown in the table. Forward slashes also work.
You can also set the same option in the Settings editor by searching for "Java Home".
Common errors
- "Java runtime could not be located": The path is wrong, points at the
javafile instead of the home directory, or the JDK is a JRE. Check that the folder containsbin/java. - Unsupported version message: The JDK is older or newer than the extension allows. Install a version from the supported list and update the setting.
- Works in the terminal but not in VS Code: VS Code was started before the environment changed. Quit VS Code fully and reopen it, or set
java.homeexplicitly. - Language server never starts after a setting change: Reload the window. The server reads the setting only at startup.
Exercise
java -version prints a valid version in your terminal, but the Apex extension still reports that it cannot find a Java runtime. Name two things you can check, and the setting that fixes it.
Show answer
Check that VS Code was restarted after the JDK was installed so it sees the new PATH and JAVA_HOME, and check that the installed Java is a supported version. To fix it directly, set salesforcedx-vscode-apex.java.home to the JDK's home directory (the folder containing bin and lib), then reload the window.
Exercise
Why is /usr/lib/jvm/java-17-openjdk-amd64/bin/java a wrong value for salesforcedx-vscode-apex.java.home?
Show answer
The setting expects the JDK home directory, not the executable. The correct value is /usr/lib/jvm/java-17-openjdk-amd64.