VS Code كبديل
في هذا الدرس 11 موضوعات
هذا الدرس متاح حاليًا باللغة الإنجليزية فقط، ولم تُنشر نسخته العربية بعد.
Lesson Overview
IntelliJ IDEA is the main tool of this course, but it is not the only way to write Java. Many developers use Visual Studio Code (VS Code), a lightweight, free editor from Microsoft that becomes a Java environment when you add extensions. You may meet it on a team, on a computer where IntelliJ is too heavy, or in a tutorial that uses it. You should know how it works and how it differs, so that you can use it without confusion.
In this lesson you will install VS Code, add the Java extensions, open a folder, write a small class, and run it. You will also compare it honestly with IntelliJ, and learn the most important rule of tool choice: pick one main tool and stay with it while you learn.
In workshop terms, IntelliJ is a fully equipped workbench, and VS Code is a light portable bench to which you attach what you need. Both use the same JDK from lesson 2. The tools on the bench do not change. Only the bench changes.
As in the last lesson, menus, extension names, and settings change between versions, and the lesson names what it could verify and hedges the rest. Use the editor's built-in search for commands (the Command Palette) when a menu looks different from the description.
Learning Objectives
By the end of this lesson you will be able to:
- Explain how VS Code differs from IntelliJ IDEA in what you get out of the box.
- Explain why VS Code needs a separate JDK and the Java extensions.
- Install VS Code and the Extension Pack for Java.
- Open a folder, create a Java class, and run it with the Run action.
- Say which of the two tools you will use in this course, and why one is enough.
- Describe how VS Code finds the JDK, and relate it to
PATHand restarting after a change.
Written Explanation
An editor that grows into an IDE
VS Code starts as a fast, small code editor. It opens folders of files, highlights text, and has a built-in terminal. It knows very little about Java by itself. Its power comes from extensions: add-ons you install from within the editor. Microsoft's documentation recommends the Extension Pack for Java, a bundle of extensions that gives VS Code its Java abilities. According to that page, the pack includes:
- Language Support for Java by Red Hat (understanding your code, completion, error marks)
- Debugger for Java
- Test Runner for Java
- Maven for Java (a build tool, which you will meet later)
- Project Manager for Java
- Visual Studio IntelliCode (smarter suggestions)
A related option is the Coding Pack for Java, which bundles VS Code, a JDK, and these extensions in one installer. Per the same page, it is available only for Windows and macOS. If you already installed the JDK in lesson 2, you do not need it.
The JDK is still required
Extensions add help to the editor, but they do not contain a compiler. Microsoft's page says you must install a Java Development Kit to run Java in VS Code, and lists JDK sources such as Amazon Corretto, Azul Zulu, Eclipse Temurin, and the Microsoft Build of OpenJDK. The extension pack supports Java 1.8 and later, per that page, and for this course you use the baseline JDK from lesson 2. So the formula is:
VS Code + Extension Pack for Java + JDK = a Java environment.
If any part is missing, something specific breaks. Without the extensions you have a plain editor with no Java help. Without the JDK the extensions have nothing to compile and run with, and VS Code will tell you that it cannot find a Java runtime.
Installing and setting up
The steps are short. Menu names may differ by version.
- Download VS Code from the official Microsoft site for your operating system, and run the installer. On Windows you may be offered options such as "Add to PATH" and adding "Open with Code" to the right-click menu. These are helpful, and you can accept them if you like.
- Open VS Code and open the Extensions view. Search for Extension Pack for Java and install it. Microsoft's page also provides a direct link to the pack in the marketplace (its identifier is
vscjava.vscode-java-pack). Install only extensions published by Microsoft or Red Hat for this purpose, and check the publisher name. - Make sure the JDK is installed. In a new terminal, run
java -versionandjavac -versionas in lesson 2. - Restart VS Code if you installed or changed the JDK while it was running. This follows the rule from lesson 3: a program gets its environment when it starts.
- Optionally, run the Java: Tips for Beginners command from the Command Palette (open it with
Ctrl+Shift+P, orCmd+Shift+Pon a Mac, and type "java tips"). Microsoft's page mentions this command as a first step.
Opening a folder and writing a class
VS Code works with folders, not with project files. The usual flow:
- Choose File, then Open Folder, and select a folder, for example
java-course/vscode-hello(create it first). - Create a new file in that folder and name it
Hello.java. As in lesson 1, the name must match the public class, and you must see the real extension. The Explorer panel on the left shows the full name, so you will notice a stray.txt. - Write the class:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from VS Code.");
System.out.println("Running on Java " + System.getProperty("java.version"));
}
}- Run it. Microsoft's documentation describes a Run | Debug link, called a CodeLens, that appears above the
mainmethod. Click Run. You can also pressF5, which starts the debugger, and the page says the debug configuration is generated automatically. Either way, the output appears in the Terminal or Debug Console panel at the bottom, depending on your settings.
The output is the same two lines you would get from javac Hello.java and java Hello in a terminal. In fact, you can do exactly that in VS Code's own integrated terminal, which opens in the folder you have opened. Do it once, and the connection between the editor and the commands becomes concrete.
Lightweight mode and standard mode
Microsoft's page mentions two working modes for Java in VS Code. Lightweight mode works with individual Java files and starts quickly. Standard mode loads the whole project and gives the full set of features, such as navigation across many files. For one-class programs, lightweight mode is enough. When you work on a project with several files and a build tool, VS Code asks you to switch, or you can do so from the status bar. The exact prompts depend on the version.
How VS Code finds the JDK
The Java extensions look for a JDK in several places. In general, they look at a Java setting in the editor, at JAVA_HOME, and at PATH, and they may also find JDKs in standard install folders. I could not verify the exact order on an official page, so do not memorize it. Use this practical approach instead:
- If it works, leave it.
- If VS Code says it cannot find Java, first check that
java -versionandjavac -versionwork in a new terminal, then restart VS Code so that it sees the environment. - If you have several JDKs and VS Code chooses the wrong one, look for the Java runtime configuration through the Command Palette (search for "Java: Configure Java Runtime"), or the Java settings. The command name is from my general knowledge, not from a page I opened, so use the search to confirm it exists in your version.
This connects directly to lessons 3 and 4: the editor is a program, it inherits its environment when it starts, and it can have its own setting that overrides the environment.
IntelliJ or VS Code: choosing
| Question | IntelliJ IDEA | VS Code with Java extensions |
|---|---|---|
| What it is | A full Java IDE | A general editor that you extend |
| Setup | One install, plus a JDK | Editor, extension pack, and a JDK |
| Weight | Heavier, needs more memory | Lighter and faster to start |
| Java depth | Deep tools designed for Java | Good and improving, with more setup |
| Other languages | Possible, but built around JVM languages | Excellent for many languages |
| In this course | The main tool, used in the videos | A fine alternative |
Neither is "better" in general. IntelliJ is the main tool in this course because the lessons show its screens, which makes it easier to follow along. VS Code is perfectly fine for learning Java. The rules of the language are identical, and the same programs run in both.
The one rule: pick one and stay with it while you learn. Switching tools whenever something goes wrong means you never learn either one, and each time you switch you rebuild the setup and may hit a different problem. You can learn the other tool later, when you know why you want it.
Examples
Example 1: The three pieces (simple)
Nora installs VS Code and opens Hello.java. She sees the text, but nothing is highlighted as a Java error and there is no Run link. She checks the Extensions view and finds that she has not installed the Extension Pack for Java. After she installs it, a Run link appears above main, and a misspelled word is underlined. The editor is the same. The extension added the Java knowledge.
Example 2: Run it two ways (practical)
Nora has the file Hello.java in her folder. She runs it twice:
- Through the editor: she clicks Run above
mainand reads the two lines in the bottom panel. - Through the terminal: she opens the integrated terminal in the same folder and types
javac Hello.java, thenjava Hello. The same two lines appear.
Nora learns that the Run link is a convenience, not a different compiler. When the link misbehaves, the terminal shows the real error. That makes the terminal her fallback. Notice also that after step 2 a Hello.class file appears in the folder. If she sees a .class file in the Explorer panel, she knows the compile step happened.
Example 3: "Java runtime not found" after an install (realistic)
Ravi installs the JDK, then opens VS Code, and the Java extension reports that it cannot find a Java runtime. He remembers lessons 2 and 3. He opens a new terminal and runs both version commands. Both work, so the JDK and PATH are fine. The VS Code window, however, was open during the install. He closes VS Code completely, reopens it, and the message is gone.
Had the commands failed in the new terminal, he would have gone back to lesson 3: find the JDK's bin folder and fix PATH. The pattern is the same for every tool: first prove the JDK works in a fresh terminal, then restart the tool that needs it.
Backend Insight
Backend developers often use more than one tool in a day: an IDE for the main code, an editor for configuration files, and a terminal for everything else. VS Code is common for editing files in many languages, such as configuration, scripts, and SQL, so you may use it alongside IntelliJ even if IntelliJ is your Java tool. That is normal, and it does not break the "pick one" rule: the rule is about where you do your Java learning, not about forbidding other editors.
It also matters that the extension approach is a trade-off. A lightweight editor that you assemble yourself gives flexibility, and a deeper IDE gives more help for large Java codebases. When you join a team, the team's build tool and project files matter more than your editor. Both IntelliJ and VS Code can open the same project, because the project lives in files such as the build configuration, not in the editor. So the habit you form here, understanding what the tool does for you and being able to reproduce it in a terminal, transfers to any editor.
Finally, remote work on servers often means an editor with no graphical Java support at all. If you can compile and run in a terminal, you are never stuck.
Common Mistakes
Mistake 1: Installing the editor but not the JDK
- Incorrect: "I installed the Extension Pack, so I can run Java."
- Correct: "I installed the JDK, verified it in a terminal, and then added the extension pack."
The pack gives help, not a compiler.
Mistake 2: Opening a single file instead of a folder
If you open only a loose file, some features and the terminal's starting folder may not be what you expect. Open the whole project folder with File, then Open Folder.
Mistake 3: Not restarting after changing the JDK or PATH
VS Code reads its environment when it starts. After installing a JDK or editing PATH, close all VS Code windows and reopen it, as with terminals in lesson 3.
Mistake 4: Installing extensions from unknown publishers
Extensions run code on your computer. Install the Extension Pack for Java from its official page and check the publisher name.
Exercises
Try each exercise yourself before opening the solutions.
Level A: Foundation
A1. What does VS Code need, in addition to the editor itself, to run Java?
أظهر الحل A1
A1. A JDK installed on the computer, and the Java extensions (normally the Extension Pack for Java). The editor alone has no Java knowledge, and the extensions have no compiler without the JDK.
A2. Name two extensions included in the Extension Pack for Java, and say what each does in one sentence.
أظهر الحل A2
A2. Examples: Language Support for Java by Red Hat provides code understanding such as completion and error marks. Debugger for Java lets you pause a program and inspect it. Test Runner for Java runs tests. Maven for Java supports a build tool. Any two are correct.
A3. Which is the course's main tool, IntelliJ or VS Code? Does the choice change the Java language you write?
أظهر الحل A3
A3. IntelliJ IDEA is the main tool. The choice does not change the language: the same code compiles and runs in both.
Level B: Application
B1. Install VS Code and the Extension Pack for Java, create a folder vscode-hello, add Hello.java from this lesson, and run it with the Run link. Write down the output lines.
أظهر الحل B1
B1. The two lines are Hello from VS Code. and Running on Java <version>, with the version of the JDK that the extensions used. Your version number depends on your JDK.
B2. In the integrated terminal of the same folder, compile and run Hello.java by hand. Which new file appears in the folder, and what does its presence prove?
أظهر الحل B2
B2. Hello.class appears. It proves the compiler ran and produced bytecode, which java Hello then ran. It also shows that the Run link and the manual commands do the same thing.
B3. Write a second class Bye in the same folder, with its own main method that prints one line. Run both classes. What do you have to be careful about with the file name?
أظهر الحل B3
B3. The file must be named Bye.java to match the public class Bye, with the same capitalization. If you declare both classes as public in the same file, the compiler refuses, so use one file per public class.
Level C: Challenge
C1. VS Code says it cannot find Java, but java -version works in a terminal that you opened a minute ago. List three things you would try, in order, and why.
أظهر الحل C1
C1. Try, in order: (1) close all VS Code windows and reopen the editor, because it may have started before the JDK or PATH change; (2) compare where java or which java in the terminal with the Java the extension reports, because there may be several JDKs; (3) look in the Java settings or the Command Palette for the Java runtime setting and point it to the JDK folder. The order goes from the cheapest and most common cause to the most specific one.
C2. Your teammate prefers VS Code and you prefer IntelliJ. They send you a project folder with a build configuration file. Explain why you can both work on it, and what you should not commit to the shared repository by accident.
أظهر الحل C2
C2. The project is defined by files such as the build configuration and the source folders, not by the editor, so both IntelliJ and VS Code can open and build it. You should not commit editor-specific files and generated folders (for example IDE settings folders and compiled output) unless the team decides to, because they are personal to a machine. Teams usually list such files in .gitignore, which lesson 1 introduced as a dot-file.
Mini Challenge
Compare the two tools on the same program, and decide which you will use.
- Create the class
Comparewith amainmethod that prints your name, the Java version, and the folder it runs from (useSystem.getProperty("user.dir")). - Run it in IntelliJ and note the three lines.
- Run it in VS Code and note the three lines.
- Write a table with these rows: time to set up, ease of finding the output, error help, and how it felt. Add one sentence for each tool.
- Decide which tool you will use for the rest of the course, and give two reasons.
Check your work: the Java version is the same in both runs or you can explain the difference, the user.dir lines differ in the way you expect (each tool runs from its own folder), and your decision names a tool.
Summary & Next Lesson
Key takeaways
- VS Code is a lightweight editor that becomes a Java environment with the Extension Pack for Java and a separately installed JDK.
- Run a class with the Run link above
main(orF5). The output appears in a panel at the bottom. - The Run link is a convenience.
javacandjavain the terminal do the same job. - Restart VS Code after installing or changing the JDK, because it reads the environment at start.
- IntelliJ is the main tool of this course. Both tools run the same language. Pick one and stay with it.
Next lesson
You now have both a terminal route and two editors. The next lesson, "Demo: installing the JDK and fixing a broken PATH", puts the whole module together: you will watch a complete setup, deliberately break PATH, read the error, make the classic mistake of testing in the old terminal, and then fix it.
Self-check
- What must you install to run Java in VS Code?
- Where do you click to run a class in VS Code?
- After installing a JDK while VS Code was open, what do you do?
- Does the editor you choose change your Java code?
- What is the rule about choosing a tool?
أظهر الإجابات
- The JDK, plus the Java extensions (the Extension Pack for Java).
- The Run link above the
mainmethod (or pressF5). - Close and reopen VS Code, and check the JDK in a new terminal.
- No. The language is the same in every editor.
- Pick one and stay with it while you learn.
References
- Microsoft, Java in Visual Studio Code. Supports the contents of the Extension Pack for Java, the Coding Pack for Java (Windows and macOS only), the requirement to install a JDK, the list of JDK sources, support for Java 1.8 and later, the Run | Debug link above
mainandF5, the Java: Tips for Beginners command, and the lightweight and standard modes. - Oracle, javac command reference (JDK 21) and java command reference (JDK 21). Support the compile and run commands used for the terminal route.
- Microsoft Learn, about_Environment_Variables. Supports the statement that child processes inherit the environment, which is why a tool needs a restart after a change.
- Not verified on an official page: the order in which the Java extensions search for a JDK, the command name "Java: Configure Java Runtime", the Command Palette shortcuts, the integrated terminal behaviour, and the panel (Terminal or Debug Console) in which output appears. The lesson hedges them. The IntelliJ and VS Code comparison table is my own summary for beginners, not a quotation from either vendor.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.