Sub topic 4
Installing a Java JDK and pointing the extensions at it
This chapter assumes you can use a terminal, have VS Code installed, and know what Apex is. You do not need to know Java. By the end you will have a working JDK, a way to confirm that VS Code can find it, and a fix for the most common failure: an Apex language server that will not start.
Why Apex tooling needs Java
Apex is not a Java program, but the Apex language server is. The language server is the background process behind code completion, go-to-definition, hover help, and live error markers. Salesforce ships it as a Java archive, so VS Code needs a Java runtime to start it.
The Salesforce CLI does not need Java for most work, such as authorizing an org or deploying source. Java matters for the editor features. If Java is missing, you can still deploy from the terminal, but the Apex editing experience in VS Code will be degraded or absent.
You need a JDK (Java Development Kit), not only a JRE (Java Runtime Environment). A JDK includes the runtime plus developer tools. Current Java distributions are normally shipped as a JDK anyway, so installing a JDK is the safe choice.
Choosing a version
The Salesforce documentation lists the Java versions the extensions support. Support has moved over time, so check the current list on the Salesforce Developers site before you install. In general:
- Choose a Long-Term Support (LTS) release. LTS releases receive updates for years, so you will not need to change JDKs often.
- Use a version that the extension documentation names as supported. A newer release that is not listed may work, but if it fails you will have no official guidance.
- Avoid installing several versions without a plan. Multiple JDKs are fine, but you must know which one VS Code picks up.
Several vendors build the same open-source Java. Any of these is acceptable:
| Distribution | Notes |
|---|---|
| Eclipse Temurin (Adoptium) | Free, widely used, installers for Windows, macOS, and Linux |
| Amazon Corretto | Free, from Amazon, long support windows |
| Azul Zulu | Free builds, many platforms |
| Oracle JDK | Check the license terms for your use before installing |
If you are unsure, pick Temurin or Corretto. The license is simple and the installers are straightforward.
Installing a JDK
Download the installer for your operating system from the vendor's site and run it. Package managers also work well and make later upgrades easier. The package names below change as versions change, so confirm the current name with the search command first.
On macOS with Homebrew:
brew search temurin
brew install --cask temurin@21On Windows with winget:
winget search Temurin
winget install EclipseAdoptium.Temurin.21.JDKOn Debian or Ubuntu:
sudo apt update
sudo apt install openjdk-21-jdkThe number 21 is an example of an LTS version. Substitute the version your extension documentation recommends.
During a Windows installation, enable the options that set JAVA_HOME and add Java to the PATH if the installer offers them. Those two settings are how most tools find Java.
Verifying the installation
Open a new terminal. A terminal that was open during installation keeps the old environment, so it may not see the new PATH.
java -versionYou should see a version line and the vendor name. Then check the compiler, which only a JDK provides:
javac -versionIf javac is not found but java is, you have a JRE only. Install a JDK.
Also check which Java the terminal picked:
echo $JAVA_HOME
which javaOn Windows PowerShell, use:
echo $env:JAVA_HOME
where.exe javaIf which java points to a different location than you expected, an older installation is earlier on your PATH. That is the usual reason a machine runs the wrong version.
What `JAVA_HOME` and `PATH` do
PATH is a list of folders the shell searches when you type a command. If the folder containing java is on PATH, then java -version works.
JAVA_HOME is an environment variable that points to the root folder of a JDK, not to the java executable. For example, it is the folder that contains bin/java, not bin/java itself. Many tools read it to find the JDK.
A common mistake is setting JAVA_HOME to the bin folder. That breaks tools that expect to append bin/java themselves.
The extension looks for Java in an order. First it uses the VS Code setting if you have set one. Then it falls back to environment variables and the PATH. This order is why an explicit setting is the most reliable fix when automatic detection fails.
Pointing the extension at a specific JDK
Set the path in VS Code settings. Open the Command Palette and run Preferences: Open User Settings (JSON), or edit the project's .vscode/settings.json to apply the setting to one project only.
{
"salesforcedx-vscode-apex.java.home": "/path/to/your/jdk"
}The value is the JDK root folder. Typical examples:
| Platform | Example path |
|---|---|
| macOS | `/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home` |
| Windows | `C:\\Program Files\\Eclipse Adoptium\\jdk-21.0.5.11-hotspot` |
| Linux | `/usr/lib/jvm/java-21-openjdk-amd64` |
Your folder name will differ depending on the exact version you installed. List the parent folder to see the real name. On Windows, JSON requires doubled backslashes, or you can use forward slashes.
On macOS, this command prints the home folder for an installed version:
/usr/libexec/java_home -v 21On Linux, this resolves the real folder behind the java command:
dirname "$(dirname "$(readlink -f "$(which java)")")"Use the output as the setting value.
User settings or workspace settings
- User settings apply to every project on your machine. Use them when you have one JDK for all Salesforce work.
- Workspace settings (
.vscode/settings.json) apply to one project. Use them when a project needs a different JDK. Be careful when committing this file, because an absolute path that works on your machine will break on a teammate's.
A path that is specific to your machine usually belongs in user settings, or in a workspace file that you add to .gitignore.
Restarting so the change takes effect
The language server reads the Java location when it starts. After you change the setting or install Java, you need to restart it.
- Open the Command Palette.
- Run Developer: Reload Window.
- Open an
.clsfile and wait for the language server to start. The status bar shows activity while it loads.
If you installed Java while VS Code was running, quit VS Code completely, not only the window, and start it again. VS Code inherits its environment when it launches, so a running instance does not see the new PATH. On macOS, launching VS Code from the Dock uses a different environment than launching from a terminal with code .. If Java works in the terminal but not in VS Code, this difference is a common cause.
Confirming that the language server runs
Use the class from the earlier setup as a quick test:
public with sharing class GreetingService {
public static String greet(String name) {
if (String.isBlank(name)) {
return 'Hello, world!';
}
return 'Hello, ' + name + '!';
}
}Open the file and check three things:
- Typing
String.shows completion suggestions such asisBlank. - Hovering over
greetshows its signature. - The Outline view in the Explorer lists the class and its method.
If those work, Java and the language server are fine. If you see only plain text coloring with no suggestions, open View > Output and choose Apex Language Server from the dropdown. Read the first error lines. They usually name the cause directly.
Common Java problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Message that Java cannot be found | No JDK installed, or VS Code started before installation | Install a JDK, then fully restart VS Code |
| Message about an unsupported Java version | JDK is older or newer than the extension supports | Install a supported LTS version and set `java.home` |
| `java -version` works in the terminal but not in VS Code | VS Code launched from the Dock or Start menu with a different environment | Set `salesforcedx-vscode-apex.java.home`, or start with `code .` |
| `javac` not found | Only a JRE is installed | Install a JDK |
| Wrong version runs | An older Java is earlier on `PATH` | Reorder `PATH` or set `java.home` explicitly |
| Server starts then stops | Setting points at the `bin` folder or a nonexistent path | Point to the JDK root and check spelling |
Keeping several JDKs
Developers often have more than one JDK for other projects. You do not have to remove them. Set java.home in VS Code to the one the Salesforce extensions should use, and leave the system default for your other work. This avoids breaking other projects when you change your global JAVA_HOME.
Tools such as SDKMAN on macOS and Linux, or the JAVA_HOME switching features of your package manager, can change versions in a terminal. They do not change what VS Code uses unless the setting or the environment it inherited changes. Remember that VS Code reads its environment at launch.
Checklist
java -versionandjavac -versionboth print a supported LTS version.JAVA_HOME, if set, points at the JDK root folder.- If automatic detection fails,
salesforcedx-vscode-apex.java.homeis set to that root folder. - VS Code was fully restarted after any change.
- An Apex file shows completion and hover information, and the Apex Language Server output has no errors.
Exercise
java -version prints a supported version in your terminal, but VS Code reports that it cannot find Java and Apex completion does not work. Name two likely causes and the single setting that avoids both.
Show answer
Likely causes: VS Code was started before Java was installed or from a launcher (such as the macOS Dock or the Windows Start menu) whose environment lacks the updated PATH and JAVA_HOME, so the extension cannot see the JDK the terminal sees; or the PATH the extension sees finds a different or unsupported Java. Setting salesforcedx-vscode-apex.java.home to the JDK root folder (not the bin folder) avoids both, because the extension uses it before searching the environment. Then fully restart VS Code.
Exercise
A teammate sets JAVA_HOME to /usr/lib/jvm/java-21-openjdk-amd64/bin/java and the language server will not start. What is wrong, and what is the correct value?
Show answer
JAVA_HOME must point to the JDK root folder, not to the executable. Tools append bin/java themselves, so the path becomes invalid. The correct value is /usr/lib/jvm/java-21-openjdk-amd64.