انتقل إلى المحتوى
أمين أبوزيد
منشورBEG-005-L02

تثبيت JDK (على أنظمة التشغيل الثلاثة)

  • الدرس 2 من 6
  • 14 دقيقة قراءة
  • مبتدئ
في هذا الدرس 11 موضوعات

هذا الدرس متاح حاليًا باللغة الإنجليزية فقط، ولم تُنشر نسخته العربية بعد.

Lesson Overview

In the last lesson you learned where files live and why the exact name of a file matters. Now you put the first real tool on the bench: the JDK, the Java Development Kit. Without it, you can read about Java, but you cannot compile or run a Java program on your own computer.

This lesson explains what the JDK contains, how it differs from the JRE that you may have heard of, and how to choose a version. Then it walks through installation on Windows, macOS, and Linux and ends with the check that matters: two commands, java -version and javac -version, both printing a version number. Finally, you will compile and run the small Greeter program from lesson 1 to prove that the whole chain works.

A note on honesty about installers: websites, installer screens, menu names, and package names change often, and they differ between vendors and operating system versions. This lesson gives you the stable ideas and the typical steps. Where an exact screen or command depends on your system, the lesson says so, and you should follow your vendor's download page for the final detail.

You will also meet the first of the module's classic mistakes: installing a runtime without the compiler. Learn it now and you will save yourself an evening later.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain what the JDK is, what it contains, and how it differs from the JRE.
  • Explain what an LTS release is and why this course uses the baseline JDK it names.
  • Choose a JDK distribution and download it from the vendor's official page.
  • Install the JDK on your own operating system.
  • Verify the installation with java -version and javac -version in a new terminal.
  • Compile and run a one-class program with javac and java.
  • Diagnose the case where java works but javac does not.

Written Explanation

What the JDK is

Java is a programming language and a platform for running programs written in it. Your Java source files (.java) are not run directly. First, the compiler (javac) translates them into .class files, which contain bytecode: instructions for the Java Virtual Machine (JVM). Then the java command starts a JVM, which runs the bytecode.

The pieces fit in a simple picture:

text
Hello.java  --javac-->  Hello.class  --java-->  program runs on the JVM
 (you write)            (compiled)              (output appears)

The JDK (Java Development Kit) is the full toolbox for developers. It contains the compiler javac, the java launcher that runs programs, the standard class libraries, and other command-line tools you will meet over time (for packaging files into a .jar, for example).

The JRE (Java Runtime Environment) was the smaller package that could run Java programs but not compile them. So the old advice was: end users install the JRE, developers install the JDK. Since JDK 11, Oracle no longer provides a separate JRE download, and the JDK is the normal thing to install. Other vendors may still offer runtime-only packages, and some Linux package managers offer a "runtime" package separately from a "development" package. That is why the rule for this course is simple:

To write and compile Java you need a JDK. A runtime-only package is not enough.

If you install a runtime-only package by mistake, java -version works but javac -version says the command is not found. That asymmetry is the sign.

Which version, and from whom

Java has a new feature release every six months, and some releases are marked LTS, for long-term support. Vendors keep updating an LTS release with fixes for years, so most companies standardize on an LTS version. This course uses one baseline JDK, an LTS release, so that everyone sees the same behaviour. Use the version named in the course materials. If you are unsure which one is current, check the vendor's download page, because I will not guess a version number for you that may be out of date by the time you read this. As examples, the Oracle installation guide I read uses JDK 21 in its sample paths, and a current JetBrains tutorial asks for Java 25 or higher; both are LTS-style choices that you may see in practice.

The JDK is built from the open-source OpenJDK project. Several organizations publish their own builds of it. Microsoft's VS Code documentation names Amazon Corretto, Azul Zulu, Eclipse Temurin, and the Microsoft Build of OpenJDK among recommended sources, and Oracle publishes its own Oracle JDK. For learning, they are interchangeable: the same language, the same java and javac commands. Pick one, install it from its official site, and do not download a JDK from a random mirror or an unofficial "download manager".

Installing on Windows

  1. Open your chosen vendor's download page and choose the Windows, 64-bit installer for the baseline version. Installers usually come as .msi or .exe files. Most home computers use a 64-bit processor. If you are unsure, Windows Settings (System, About) shows the system type.
  2. Run the installer and follow the steps. Note the folder it installs to. Oracle's documentation gives C:\Program Files\Java\jdk-<version> as its default location. Other vendors use different folders, such as one under C:\Program Files\Eclipse Adoptium or C:\Program Files\Microsoft. You will need this folder in lesson 3.
  3. Look for installer options such as "Add to PATH" and "Set JAVA_HOME variable". Where the installer offers them, enabling them is the easy path. Some installers set PATH by default and some do not, so you must check by running the commands.
  4. Open a new terminal (Command Prompt or PowerShell) and run the two version commands.

Installing on macOS

  1. Download the installer package for your Mac's processor type. Oracle's documentation says macOS is supported for both Intel (x64) and Apple Silicon (AArch64) Macs. If you do not know which you have, the Apple menu, then "About This Mac", shows it.
  2. Run the package and follow the steps. Installers on macOS normally place the JDK in a system folder for Java virtual machines, and the java command is found automatically on most setups. Some distributions also install through a package manager, such as Homebrew. If you use one, follow that vendor's instructions, because a package manager may install the JDK in a folder it manages and may need extra steps to make it the default.
  3. Open a new Terminal and run the two version commands.

Installing on Linux

On Linux, the usual way is your distribution's package manager, which downloads and installs the JDK from the distribution's own repositories. The command and package name depend on the distribution and version. For example, Debian and Ubuntu systems typically use apt with a package named like openjdk-<version>-jdk, and Fedora-family systems use dnf with java-<version>-openjdk-devel. Check your distribution's documentation for the exact name, and make sure the package ends in jdk or devel, not only jre or runtime.

A second way is to download an archive (a .tar.gz file) from a vendor, unpack it to a folder you choose, and add that folder's bin directory to your PATH, which you will do in lesson 3.

Installing system packages usually needs administrator rights, so the command often starts with sudo. Type it only if you understand what the command installs.

Checking the installation

Open a new terminal window. This matters, and lesson 3 explains why. Then run:

text
java -version
javac -version

You should see a version line for each. The java output typically looks like this (the numbers and the vendor name will differ on your machine):

text
openjdk version "<version>" <date>
OpenJDK Runtime Environment (<vendor build>)
OpenJDK 64-Bit Server VM (<vendor build>, mixed mode)

and the javac output is a single line:

text
javac <version>

Three small facts help you read the result:

  • javac -version prints just the compiler's version, according to Oracle's documentation. The same page says javac --version works too.
  • Oracle's documentation for java says that -version prints the version to the error stream, while --version prints to the standard output stream. For you, both appear in the terminal. The difference matters later when a script or tool captures the output.
  • The two version numbers should match. If java reports one version and javac another, two JDKs are installed, and your system is mixing them. Lesson 3 shows how to find out which one is first on PATH.

The first real test: compile and run

Version numbers prove that the tools are present. To prove that they work, compile and run the program from lesson 1. Open a terminal in the greeter folder, which contains Greeter.java, and run:

text
javac Greeter.java
java Greeter

The first command writes Greeter.class into the folder and prints nothing if all is well. The second command starts the JVM, runs the class, and prints Workshop ready.. Notice that you type java Greeter with the class name and no .class or .java. The documented form is java [options] mainclass, so the launcher expects the class's name.

Examples

Example 1: Reading a version line (simple)

A learner sees this after running java -version:

text
openjdk version "<version>" <date>

Reading it: openjdk says the build is based on OpenJDK, the quoted text is the Java version, and the date is the release date of that build. The learner checks the quoted number against the version in the course materials. If the number matches, step one is done. Then the learner runs javac -version, which prints javac <version>. The two numbers match, so the JDK and its compiler are the same installation.

Example 2: Installing on a clean Windows laptop (practical)

Amira has a new Windows laptop with no Java.

  1. She opens cmd and runs java -version. The reply says the command is not recognized. That is expected: there is no Java yet.
  2. She downloads a 64-bit installer from her chosen vendor's official page. The installer lists options, including "Add to PATH". She enables the options that add the JDK to PATH and set JAVA_HOME.
  3. After the install, she runs java -version again in the same window. It still fails. That is because the window was opened before the change. She closes it and opens a new Command Prompt.
  4. In the new window both commands print the same version.
  5. She opens the greeter folder in the terminal, runs javac Greeter.java, then java Greeter, and sees Workshop ready..

Step 3 is not a bug. It is the behaviour lesson 3 explains, and it is the third classic mistake of this module.

Example 3: java works, javac does not (realistic)

Karim installs Java on Linux and wants to be quick. He runs the package manager and picks a package with runtime in its name. Then:

text
$ java -version
openjdk version "<version>" <date>
...
$ javac -version
bash: javac: command not found

java found a runtime, but there is no compiler. His next move is to install the development package, which in his distribution has a name ending in jdk or devel. Then he opens a new terminal, runs both commands, and gets matching versions.

The lesson from Karim: the pair of commands is the check. Running only java -version tells you that a runtime exists. Running javac -version tells you that you have what a developer needs.

Backend Insight

Real backend projects state which Java version they need, because code compiled and tested on one version may behave differently, or fail, on another. A team normally agrees on one JDK version for the project, and the servers that run the program use a matching runtime. When a build fails on a colleague's laptop with an error about "unsupported version", the first thing people check is java -version and javac -version.

Servers and build pipelines also install a JDK, often from the same vendors you saw here, usually through a script or a container image rather than a graphical installer. The skills you practise today (picking a version, verifying it from the command line, noticing a mismatch) are exactly what such scripts automate. In a later module on environment differences, you will see the same checks in a new setting.

One more habit that carries over: print the versions first whenever something Java-related fails. It takes five seconds and rules out a large group of causes.

Common Mistakes

Mistake 1: Installing a JRE (runtime only) instead of a JDK

  • Incorrect: "java -version works, so Java is installed."
  • Correct: "java -version and javac -version both work, so the JDK is installed."

Mistake 2: Several JDKs, and the wrong one is used

If you install a second JDK without removing the first, one of them is found before the other. The java and javac commands may then come from different installations and report different versions. Check that both numbers agree, and uninstall versions you do not need (or fix the order of folders on PATH, lesson 3).

Mistake 3: Testing in a terminal that was open during the install

A terminal gets its settings when it starts. If you installed Java while the terminal was open, the terminal may not see the new PATH. Close it, open a new one, and test again.

Mistake 4: Typing java Greeter.class after compiling

  • Incorrect: java Greeter.class. The launcher expects a class name, not a file name.
  • Correct: java Greeter.

Exercises

Try each exercise yourself before opening the solutions.

Level A: Foundation

A1. Explain in one sentence each: JDK, JRE, JVM, javac.

أظهر الحل A1

A1. JDK: the toolbox for developers, with the compiler, the launcher, the libraries, and tools. JRE: the older, smaller package that can run Java programs but not compile them. JVM: the virtual machine that runs compiled bytecode. javac: the compiler that turns .java source files into .class files.

A2. What does java -version tell you, and what does javac -version tell you that it does not?

أظهر الحل A2

A2. java -version shows that a Java runtime is found and which version it is. javac -version shows that the compiler is found and its version. A machine with only a runtime passes the first and fails the second.

A3. Why should you open a new terminal after installing?

أظهر الحل A3

A3. A terminal reads its settings when it starts, so a terminal that was already open may not know about the change. A new terminal sees the current settings.

Level B: Application

B1. Install the baseline JDK on your computer from the vendor's official page (or your package manager on Linux). Run both version commands in a new terminal and write down exactly what each printed.

أظهر الحل B1

B1. Your output depends on the vendor and version. Success is that both commands print a version line, and that the numbers match each other and the course's baseline. Include the vendor name and any installer options you chose.

B2. A classmate shows you java -version printing a version, and javac -version printing "command not found". List two possible causes and the next action for each.

أظهر الحل B2

B2. Cause 1: only a runtime was installed. Next action: install the development package or full JDK, open a new terminal, and test again. Cause 2: the folder with javac is not on PATH, even though another java is found. Next action: find where the JDK's bin folder is and check PATH (lesson 3). A third possibility: the terminal was opened before the install.

B3. Compile and run Greeter.java from lesson 1. Then change the message in the file to say Hello from my JDK., compile again, and run again. What do you have to do after editing the source and before running?

أظهر الحل B3

B3. After editing the source, you must compile again with javac Greeter.java so that a new Greeter.class is written. If you skip it, java Greeter still runs the old compiled class and prints the old message.

Level C: Challenge

C1. Your machine prints openjdk version with one number for java -version and a different number for javac -version. Explain what has happened, give two ways to investigate, and say what you would do to resolve it.

أظهر الحل C1

C1. Two JDK installations are on the machine, and the java command comes from one while javac comes from another, because their folders appear in PATH in an order that picks them separately, or because one came from a system package and another from an installer. Ways to investigate: print PATH and look at which JDK folders are listed, and use the system's command for locating a command (where java and where javac on Windows; which java and which javac on macOS and Linux). To resolve it, uninstall the JDK you do not want, or put the folder of the wanted JDK first on PATH, then open a new terminal and check that both numbers match.

Mini Challenge

Document your installation as a short "setup card" that you could hand to a classmate.

  1. Write the vendor, the version, and the date you installed.
  2. Write the folder where the JDK is installed. On Windows, look in the installer's final screen or in C:\Program Files; on other systems, use the vendor's instructions or the output of which java. Do not guess.
  3. Paste the exact output of java -version and javac -version.
  4. Write the three commands to compile and run Greeter.java, and the output you got.
  5. Add a "if it breaks" line: what you would check first.

Check your work: both version numbers on the card match, the card names the folder, and a classmate could repeat your steps without asking you anything.

Summary & Next Lesson

Key takeaways

  • The JDK is the developer's toolbox: compiler (javac), launcher (java), libraries, and tools. A JRE alone cannot compile.
  • Pick the baseline LTS version named by the course, from an official vendor site or your distribution's repositories.
  • On Windows and macOS you usually use an installer. On Linux you usually use the package manager, choosing a package that ends in jdk or devel.
  • The check is two commands in a new terminal, java -version and javac -version, with matching version numbers.
  • To compile and run: javac Greeter.java, then java Greeter (class name only).
  • Compile again after every edit to the source.

Next lesson

You installed the JDK and the commands worked, but you may not yet know why they work from any folder. The next lesson, "Environment variables and PATH", explains the list of folders your terminal searches, which is the knowledge you need when "command not found" appears.

Self-check

  1. Which command shows that the compiler is installed?
  2. What is the difference between a JDK and a JRE?
  3. You installed Java with a terminal already open and java is not found. What do you try first?
  4. What do you type to run the compiled class Greeter?
  5. java reports one version and javac another. What does that suggest?
أظهر الإجابات
  1. javac -version.
  2. The JDK contains the compiler and developer tools in addition to what is needed to run programs. A JRE can only run programs.
  3. Close the terminal, open a new one, and run the command again.
  4. java Greeter, the class name without an extension.
  5. Two JDK installations are involved, and the system finds them in a mixed order.

References

  • Oracle, JDK 21 installation overview. Supports the list of supported systems (Windows 64-bit, Linux, macOS on Intel and Apple Silicon) and Oracle's default Windows install folder C:\Program Files\Java\jdk-<version>. The page does not itself describe PATH or JAVA_HOME; those points come from lesson 3's sources and general practice.
  • Oracle, javac command reference (JDK 21). Supports what javac does and that javac -version and javac --version both print the compiler version.
  • Oracle, java command reference (JDK 21). Supports the form java [options] mainclass, and that -version prints to the error stream while --version prints to standard output.
  • OpenJDK bug tracker, JDK-8210614. A search summary indicated that, from JDK 11, Oracle no longer offers a separate JRE download. I did not open this page directly, so treat that statement as reported, not verified, and check your vendor if it matters to you.
  • Microsoft, Java in Visual Studio Code. Supports the list of JDK sources (Amazon Corretto, Azul Zulu, Eclipse Temurin, Microsoft Build of OpenJDK) and that a JDK must be installed to run Java in VS Code.
  • JetBrains, Create your first Java application. Supports the mention that a current IntelliJ tutorial asks for a recent JDK (Java 25 or higher at the time I read it).
  • Not verified in this session: the exact package names on Debian/Ubuntu and Fedora (openjdk-<version>-jdk, java-<version>-openjdk-devel), Homebrew behaviour on macOS, and the installer option names, which vary by vendor. They are hedged in the text.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

المختبر والتمارين الاختبار

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.