נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
1
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

מה זה תכנות? JVM, JDK, IntelliJ ו-jshellWhat is programming? JVM, JDK, IntelliJ & jshell

איך מחשב מבצע הוראות, איך Java רצה (קוד, javac, bytecode, JVM), התקנת JDK ו-IntelliJ ב-Windows וב-Mac, jshell, והתוכנית הראשונה.

How a computer follows instructions, how Java runs (source, javac, bytecode, the JVM), installing the JDK and IntelliJ on Windows and Mac, jshell, and a first program.

התקנה ב-Windows, צעד אחר צעדWindows installation, step by step

  1. נכנסים ל-adoptium.net, בוחרים Temurin 21 (LTS) ו-Windows x64, ומורידים את קובץ ה-.msi.Go to adoptium.net, choose Temurin 21 (LTS) and Windows x64, and download the .msi file.
  2. מריצים את הקובץ. במסך Custom Setup לוחצים על Set JAVA_HOME variable ובוחרים Will be installed on local hard drive. ודאו שגם Add to PATH מסומן.Run it. On the Custom Setup screen click Set JAVA_HOME variable and choose Will be installed on local hard drive. Make sure Add to PATH is on too.
  3. Next ← Install, ומחכים לסיום.Next → Install, and wait for it to finish.
  4. סוגרים ופותחים מחדש את PowerShell (חלון ישן לא מכיר את ההתקנה החדשה), ובודקים:Close and reopen PowerShell (an old window doesn't know about the new install), and check:
PowerShell
java --version
javac --version
echo $env:JAVA_HOME
פלט לדוגמהSample output
openjdk 21.0.5 2024-10-15 LTS
OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode, sharing)
javac 21.0.5
C:\Program Files\Eclipse Adoptium\jdk-21.0.5.11-hotspot\
שימו לבNote

אם javac "is not recognized" גם בחלון חדש: מחפשים בתפריט התחל Edit the system environment variables ← Environment Variables ← ב-Path בודקים שיש שורה שנגמרת ב-\bin של תיקיית ה-JDK. אם אין, מוסיפים אותה (New) ופותחים PowerShell חדש.

If javac "is not recognized" even in a new window: search the Start menu for Edit the system environment variables → Environment Variables → in Path check there's a line ending in the JDK folder's \bin. If not, add it (New) and open a new PowerShell.

התקנה ב-Mac, צעד אחר צעדMac installation, step by step

  1. ב-adoptium.net בוחרים Temurin 21 (LTS) ו-macOS: aarch64 למק עם שבב Apple (M1 ומעלה), x64 למק עם Intel. לא בטוחים? תפריט ← About This Mac ← Chip.On adoptium.net choose Temurin 21 (LTS) and macOS: aarch64 for an Apple-chip Mac (M1 or later), x64 for an Intel Mac. Not sure? Apple menu → About This Mac → Chip.
  2. פותחים את קובץ ה-.pkg ועוברים על המסכים (Continue, Agree, Install).Open the .pkg file and go through the screens (Continue, Agree, Install).
  3. פותחים Terminal ובודקים:Open Terminal and check:
Terminal
java --version
javac --version
פלט לדוגמהSample output
openjdk 21.0.5 2024-10-15 LTS
OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode, sharing)
javac 21.0.5
שימו לבNote

אם מופיעה גרסה אחרת (למשל 17 או 1.8), יש כמה JDK במחשב. רואים את כולם עם /usr/libexec/java_home -V, ובוחרים את 21 עם export JAVA_HOME=$(/usr/libexec/java_home -v 21) (אפשר להוסיף את השורה לקובץ ~/.zshrc).

If another version shows up (say 17 or 1.8), there are several JDKs on the Mac. See them all with /usr/libexec/java_home -V, and pick 21 with export JAVA_HOME=$(/usr/libexec/java_home -v 21) (you can add that line to ~/.zshrc).

IntelliJ IDEA CommunityIntelliJ IDEA Community

  1. מורידים מ-jetbrains.com/idea/download את IntelliJ IDEA Community Edition (גוללים מעט למטה; Ultimate בתשלום) ומתקינים.Download IntelliJ IDEA Community Edition from jetbrains.com/idea/download (scroll down a little; Ultimate is paid) and install it.
  2. בפתיחה הראשונה: New Project. שם: java-course. Language: Java. Build system: IntelliJ. JDK: 21. מבטלים את Add sample code (נכתוב בעצמנו).On first launch: New Project. Name: java-course. Language: Java. Build system: IntelliJ. JDK: 21. Untick Add sample code (we'll write our own).
  3. אם JDK 21 לא ברשימה: Add JDK ← בוחרים את התיקייה שבה הוא הותקן. או Download JDK ← Eclipse Temurin 21.If JDK 21 isn't listed: Add JDK → pick the folder where it's installed. Or Download JDK → Eclipse Temurin 21.
  4. קליק ימני על src ← New ← Java Class ← Main. בפנים: psvm ואז Tab, ואחר כך sout ואז Tab.Right-click src → New → Java Class → Main. Inside: psvm then Tab, then sout then Tab.
  5. מריצים עם המשולש הירוק שליד main, או Ctrl+Shift+F10 (Windows) / Ctrl+Shift+R (Mac).Run with the green triangle next to main, or Ctrl+Shift+F10 (Windows) / Ctrl+Shift+R (Mac).
קיצורShortcutמה הוא כותבWhat it writes
psvm + Tabpublic static void main(String[] args) { }
sout + TabSystem.out.println();
souf + TabSystem.out.printf("");
Alt+Enter (Mac: Option+Enter)Alt+Enter (Mac: Option+Enter)הצעת תיקון לשגיאה המסומנת באדוםSuggests a fix for the error underlined in red

חלופה: VS CodeAlternative: VS Code

מעדיפים עורך קל? מתקינים VS Code, ואז מחלון התוספים (Ctrl+Shift+X) את Extension Pack for Java של Microsoft. פותחים את התיקייה java-course, יוצרים Main.java, ומעל main מופיע הקישור Run. ההוראות המלאות: Java ב-VS Code.

Prefer a light editor? Install VS Code, then from Extensions (Ctrl+Shift+X) install Microsoft's Extension Pack for Java. Open the java-course folder, create Main.java, and a Run link appears above main. Full instructions: Java in VS Code.

איך מריצים את הקוד של המפגשRunning this session's code

הפלט שמופיע ליד כל דוגמה הוא הפלט האמיתי שלה מהרצה על JDK 21. בתרגילים כותבים קוד בעורך שבעמוד, מעתיקים ל-IntelliJ ומריצים שם, ומדביקים את הפלט בחזרה: האתר משווה אותו לפלט של הפתרון שלנו. במחשב: ב-IntelliJ, או בטרמינל:

The output next to every example is its real output from a run on JDK 21. In the exercises you write code in the page's editor, copy it to IntelliJ and run it there, then paste the output back: the site compares it with our solution's output. On your computer: in IntelliJ, or in a terminal:

פעולהActionMacMacWindowsWindows
קומפילציהCompilejavac Main.javajavac Main.java
הרצהRunjava Mainjava Main
קובץ אחד בבת אחתOne file in one gojava Main.javajava Main.java
לנסות שורותTry lines outjshelljshell

פתרון בעיותTroubleshooting

בעיהProblemסיבהCauseפתרוןFix
'javac' is not recognized (Windows)'javac' is not recognized (Windows)ה-JDK לא ב-PATH, או שהחלון נפתח לפני ההתקנהThe JDK isn't on PATH, or the window was opened before installingפותחים PowerShell חדש. אם לא עוזר: מוסיפים את bin של ה-JDK ל-Path (ראו למעלה)Open a new PowerShell. If that doesn't help, add the JDK's bin to Path (see above)
zsh: command not found: javac (Mac)zsh: command not found: javac (Mac)ה-JDK לא הותקן, או שהותקן רק JRENo JDK is installed, or only a JREמתקינים Temurin 21 מ-adoptium.net ופותחים Terminal חדשInstall Temurin 21 from adoptium.net and open a new Terminal
class Main is public, should be declared in a file named Main.javaclass Main is public, should be declared in a file named Main.javaשם הקובץ לא זהה לשם המחלקהThe file name doesn't match the class nameמשנים את שם הקובץ או את שם המחלקה כך שיהיו זהים, כולל אותיות גדולותRename the file or the class so they match, capitals included
Error: Could not find or load main class Main.classError: Could not find or load main class Main.classהריצו java עם שם קובץjava was given a file namejava Main, בלי .classjava Main, without .class
error: file not found: Main.javaerror: file not found: Main.javaהטרמינל לא בתיקייה של הקובץThe terminal isn't in the file's foldercd לתיקייה הנכונה, ובודקים עם ls (או dir)cd to the right folder and check with ls (or dir)
עברית מופיעה כסימני שאלה בטרמינל של WindowsHebrew shows as question marks in the Windows terminalהטרמינל לא מוגדר ל-UTF-8The terminal isn't set to UTF-8ב-IntelliJ זה עובד מיד. ב-PowerShell: chcp 65001 לפני ההרצהIt works right away in IntelliJ. In PowerShell: chcp 65001 before running
IntelliJ: "Project JDK is not defined"IntelliJ: "Project JDK is not defined"לא נבחר JDK לפרויקטNo JDK was chosen for the projectFile ← Project Structure ← SDK: בוחרים 21File → Project Structure → SDK: pick 21

נבנה על ידיBuilt by Ariel Yohimovich