Sub topic 1
How VS Code, the Salesforce CLI, Java, and the extensions fit together
Setting up Apex development in VS Code means installing four separate things. Most setup failures come from not knowing which piece does which job. This chapter gives you the working model before you install anything. Later chapters cover each installation step by step.
Prerequisites
This chapter assumes you have:
- Basic familiarity with Visual Studio Code, such as opening folders, using the Command Palette, and opening the integrated terminal.
- Comfort running commands in a terminal and reading their output.
- A Salesforce org you are allowed to develop in, such as a Developer Edition org or a sandbox. Do not use a production org for practice.
- A basic idea of what Apex is: Salesforce's Java-like language for server-side logic, stored as
.cls(class) and.triggerfiles.
You do not need to install anything to read this chapter.
The four pieces and what each one does
Salesforce does not ship a single "Apex IDE" for VS Code. The experience comes from four components that cooperate.
| Component | What it is | Main job |
|---|---|---|
| Visual Studio Code | A general-purpose code editor | Hosts the editor, file explorer, terminal, test view, and debugger user interface |
| Salesforce CLI (`sf`) | A command-line program | Talks to Salesforce orgs: logs in, deploys, retrieves, runs tests, runs anonymous Apex |
| Java JDK | The Java Development Kit, which provides the Java runtime | Runs the Apex language server |
| Salesforce Extension Pack | A bundle of VS Code extensions | Connects the editor to the CLI and the language server, and adds menus, commands, and debuggers |
VS Code by itself knows nothing about Apex or Salesforce. The extensions add that knowledge. The extensions in turn rely on two programs installed on your machine, the CLI and Java. If either is missing, a different set of features stops working.
Two separate paths: the CLI path and the Java path
It helps to think of two independent paths that start in the extensions.
- The CLI path handles anything that touches an org. When you run a command such as SFDX: Deploy This Source to Org, the extension builds a
sfcommand, runs it, and shows you the output. - The Java path handles anything that is about understanding your code. The Apex language server is a Java program. The Apex extension starts it in the background, and it reads your project files to power code completion, go-to-definition, and error squiggles.
These paths fail independently. If Java is missing, you can still deploy code because deployment uses the CLI, but you lose completion and diagnostics. If the CLI is missing or you have no authorized org, the editor still understands your Apex, but you cannot deploy or test.
What the extensions actually do
The Salesforce Extension Pack is a collection of individual extensions installed together. You rarely need to think about them one by one, but a few matter for this tutorial.
- The core extension (Salesforce CLI Integration) adds the
SFDX:commands to the Command Palette, the status bar item showing your default org, and the deploy and retrieve context menus. - The Apex extension starts the language server and adds completion, navigation, and diagnostics for
.clsand.triggerfiles. It also adds the test integration and the SFDX: Execute Anonymous Apex commands. - The Apex Replay Debugger extension reads a debug log you captured from an org and lets you step through it in the VS Code debugger.
- Other extensions in the pack cover Lightning Web Components, Visualforce, and SOQL. They are not the focus here.
The exact membership of the pack can change between releases. Treat the list above as the parts relevant to Apex, and check the pack's page in the Extensions view for the current list.
The command names still begin with SFDX: even though the current CLI executable is sf. This is a historical naming leftover, not a sign that you have the wrong CLI.
The project is the shared context
Neither the CLI nor the extensions work well in an arbitrary folder. Both look for a Salesforce DX project, which is a folder containing an sfdx-project.json file at its root. That file tells the tools where your source lives and which API version to use.
The extensions use the file as a signal. When VS Code opens a folder that contains sfdx-project.json, the Salesforce extensions activate and the SFDX: commands become available. The CLI also reads it, so commands such as sf project deploy start know which directories to deploy. The language server uses the package directories listed in it to find the Apex files to index.
A typical file looks like this:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"name": "my-apex-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Chapter-by-chapter, you will create this project and learn each field. For now, the point is that the project folder is what ties the editor, CLI, and language server to the same set of files.
The org connection is stored by the CLI
An org is not something VS Code connects to directly. You authorize an org once through the CLI, which stores the credentials on your machine. After that, both terminal commands and extension commands use the same stored authorization.
The two settings you will use most are the org alias and the default org. A project can have a default org set, and the extension shows it in the VS Code status bar. Extension commands act on that org unless told otherwise.
sf org login web --alias my-org --set-default
sf org listBecause the CLI owns the login, anything you do in the editor can also be done in a terminal, and the reverse. This is useful when you debug a problem: if a command fails in the editor, run the equivalent sf command in the terminal and read the raw error.
How a typical action flows through the stack
Following one action end to end shows where each component takes part. Take "deploy this class to my org":
- You right-click
GreetingService.clsand choose SFDX: Deploy This Source to Org. - The core extension builds a
sf project deploy startcommand for that file and runs it in the background. - The CLI reads
sfdx-project.json, finds your default org's stored credentials, and calls Salesforce APIs. - Salesforce compiles the Apex on the server. This is the authoritative compile. If it finds an error, the CLI reports it.
- The extension prints the CLI output in the Output panel and, for compile errors, marks the file in the Problems list.
Java is not involved in that flow. The language server's squiggles while you type are a faster, local check. They help you catch mistakes early, but the org's compile is what decides whether a deploy succeeds.
What to verify before going further
Before installing the extensions, confirm that the three programs outside the extensions are available on your PATH, the list of folders your terminal searches for commands.
code --version
sf --version
java -versionEach command should print a version number. If one prints "command not found" (or a similar message), the matching component is not installed or is not on your PATH. That is the first thing to fix, and later chapters cover each installation.
Two details are worth knowing now:
- The
codecommand is added to yourPATHby the VS Code installer on some platforms, and on others you enable it from the Command Palette ("Shell Command: Install 'code' command in PATH" on macOS). - A terminal opened before an installation does not see the new
PATH. Open a new terminal, and sometimes restart VS Code, after installing anything.
Where Java shows up in settings
Usually the Apex extension finds Java by itself. When it cannot, you point it at a JDK with a VS Code setting:
{
"salesforcedx-vscode-apex.java.home": "/path/to/your/jdk"
}Remember the symptom pattern, because it recurs in troubleshooting. If completion, hover, and go-to-definition do not work but deploys do, suspect Java. If the status bar shows no org or deploys fail with an authorization error, suspect the CLI login.
Common misconceptions
- "The extension deploys code itself." It does not. It runs the CLI, so a broken or outdated CLI breaks deploys even if the extension is fine.
- "Java is only needed to run my Apex." Apex runs on Salesforce servers, not on your machine. Java is needed only for the language server that powers editing features.
- "Installing the extension pack installs everything." It installs only VS Code extensions. The CLI and the JDK are separate programs you install yourself.
- "If it works in the terminal, it works in the editor." Not always. VS Code may have started with a different
PATHthan your terminal, so the editor can fail to find a tool the terminal finds.
Summary table: symptom to likely component
| Symptom | Likely component |
|---|---|
| No `SFDX:` commands in the Command Palette | Extension pack not installed, or folder is not a DX project |
| No completion or go-to-definition for Apex | Java missing or not found by the language server |
| Deploy or test commands fail with an authorization error | No authorized org, or the default org is not set |
| `sf: command not found` in the terminal | CLI not installed or not on `PATH` |
Exercise
You open a DX project in VS Code. Typing Apex gives no completion and no hover information, but right-clicking a class and choosing SFDX: Deploy This Source to Org succeeds. Which component is the most likely cause, and why does the deploy still work?
Show answer
Java is most likely missing or not found. Completion and hover come from the Apex language server, which runs on Java. Deployment goes through the Salesforce CLI to the org, which does not use Java, so the deploy works independently.
Exercise
Your teammate says, "I installed the Salesforce Extension Pack, so I can deploy now." What else must be in place for a deploy to work?
Show answer
The Salesforce CLI must be installed and on the PATH, the folder must be a Salesforce DX project with sfdx-project.json, and an org must be authorized and set as the default. The extension pack only adds VS Code extensions. It does not install the CLI or a JDK.