Sub topic 13
Common setup problems and useful settings
This chapter assumes you have finished the earlier setup steps: VS Code, the Salesforce CLI, a Java JDK, the Salesforce Extension Pack, a Salesforce DX project, and an authorized org. Most setup failures trace back to two of those pieces: the Java runtime that the Apex language server needs, and the org authorization that deploys and tests need.
How problems usually surface
The two failure areas show up in different ways. A Java problem affects editing. An authorization problem affects anything that talks to the org.
Problem 1: the Apex language server will not start
The Apex language server provides completion, navigation, and diagnostics. It runs on Java. If VS Code cannot find a suitable JDK, you will see a message that Java is missing, or code completion simply never appears.
Check Java first:
java -versionIf the command is not found, install a JDK and reopen VS Code so it picks up the new PATH. If Java is installed but VS Code still cannot find it, point the extension at it directly. Open Preferences: Open User Settings (JSON) from the Command Palette and add:
{
"salesforcedx-vscode-apex.java.home": "/path/to/your/jdk"
}Use the JDK's home directory, not the path to the java executable. On macOS and Linux, a command such as /usr/libexec/java_home (macOS) or the folder that contains bin/java helps you find it. After you change the setting, run Developer: Reload Window.
Two further points:
- Use a JDK version the Salesforce extensions support. Check the current extension documentation, because the supported versions change over time.
- A JRE alone may not be enough. Install a full JDK.
Problem 2: the org is not authorized or not the default
Deploy, retrieve, test, and log commands all target an org. If none is set as the default, or the saved login has expired, the commands fail with an error about a missing or invalid org.
List the orgs the CLI knows about:
sf org listLook at the output for your alias and its status. If the org is missing or expired, authorize it again and set it as the default:
sf org login web --alias my-org --set-defaultThe default org is also shown in the VS Code status bar. If it shows the wrong org, run SFDX: Set a Default Org from the Command Palette. Also confirm that you opened the project folder itself, the one containing sfdx-project.json. Opening a parent folder prevents the extensions from detecting a DX project.
Problem 3: the CLI works in the terminal but not in VS Code
VS Code reads the PATH that was active when it started. If you installed the CLI or Java while VS Code was open, the editor may not see them. Quit VS Code fully and reopen it. Then confirm all three tools respond:
code --version
sf --version
java -versionUseful settings
Settings can be set per user or per workspace. Workspace settings live in .vscode/settings.json inside the project and apply only to that project.
| Setting | Purpose |
|---|---|
| `salesforcedx-vscode-apex.java.home` | Selects the JDK used by the Apex language server. |
| `salesforcedx-vscode-core.push-or-deploy-on-save.enabled` | Deploys a file to the org each time you save it. Use with care on shared orgs. |
Deploy on save is convenient in a scratch org or Developer Edition org. In a shared sandbox it can overwrite other people's work, so leave it off there.
Where to look for details
When a command fails, open the Output panel and choose the relevant channel from the drop-down, such as Apex Language Server or Salesforce CLI. The messages there are more specific than the pop-up notifications. Copy the exact error text when you search the documentation or ask for help.
Exercise
Code completion never appears in your .cls files, but sf org list shows a valid default org. Which setting do you check first, and what value does it need?
Show answer
Check salesforcedx-vscode-apex.java.home. It must be the home directory of an installed, supported JDK (the folder that contains bin/java), not the path to the java executable. Reload the window after changing it.
Exercise
A deploy fails with an error that no default org is set, although you authorized an org yesterday. Which two commands or actions help you diagnose and fix it?
Show answer
Run sf org list to see whether the org is still listed and authorized. If it is expired or not the default, run sf org login web --alias my-org --set-default, or use SFDX: Set a Default Org.