JavaJava · Chapterبەش 3 · Lectureوانە 3.3

Compiling and Running: javac, Bytecode and the JVMکۆمپایلکردن و جێبەجێکردن: javac، بایتکۆد و ئامێری گریمانەیی Java

The practice of compile-and-run: saving ClassName.java, compiling with javac, running with java, a whole terminal session, what is inside a .class file (javap, CAFEBABE), the class loader and the bytecode verifier, Run buttons and TextPad, java Hello.java, JShell and the commonest command mistakes.

کاری کۆمپایل و جێبەجێکردن: پاشەکەوتکردنی ClassName.java، کۆمپایلکردن بە javac، جێبەجێکردن بە java، دانیشتنێکی تەواوی ، ناوەوەی فایلێکی .class (javap، CAFEBABE)، و ، دوگمەکانی Run و TextPad، java Hello.java، JShell و باوترین هەڵەکانی .

12 min read13 خولەک خوێندنەوە · 14 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Save a program under the right name, compile it with javac and run it with java in a terminal.
  • Explain what bytecode and a .class file are, and look inside one with javap -c.
  • Describe what the class loader and the bytecode verifier do before a program runs.
  • Use an IDE's Run button, java Hello.java and JShell, and fix the commonest command mistakes.
  • بە ناوی دروست پاشەکەوت بکەیت، بە javac کۆمپایلی بکەیت و بە java لە جێبەجێی بکەیت.
  • ڕوونی بکەیتەوە و فایلی .class چین و بە javap -c سەیری ناوی یەکێکیان بکەیت.
  • وەسفی ئەوە بکەیت کە و پێش جێبەجێبوونی چی دەکەن.
  • دوگمەی Runـی ، java Hello.java و JShell بەکار بهێنیت و باوترین هەڵەکانی چاک بکەیتەوە.

Write, compile, runبنووسە، کۆمپایل بکە، جێبەجێ بکە

Every Java program goes through the same cycle, and you need a JDK installed for it (Install a JDK). You write the source code in any text editor or IDE, save it as a .java file, compile it with javac, and run the bytecode with java.

هەموو Java بە هەمان خولدا تێدەپەڕێت و بۆ ئەمە جێگیرکراوت پێویستە (کەرەستەیەکی گەشەپێدانی Java جێگیر بکە). لە هەر یان IDEـێکدا دەنووسیت، وەک فایلێکی .java پاشەکەوتی دەکەیت، بە javac کۆمپایلی دەکەیت و بە java جێبەجێ دەکەیت.

A cycle of four boxes: write or edit Welcome.java in an editor; save it on the disk; compile it with javac Welcome.java, which makes Welcome.class; run it with java Welcome, which shows the result. Two arrows lead back to the editor: one from javac labelled compile errors, one from the result labelled runtime errors or a wrong result.خولێکی چوار چوارچێوەیی: نووسین یان دەستکاریکردنی Welcome.java لە دەستکاریکەرێکدا؛ پاشەکەوتکردنی لەسەر دیسک؛ کۆمپایلکردنی بە javac Welcome.java کە Welcome.class دروست دەکات؛ جێبەجێکردنی بە java Welcome کە ئەنجامەکە پیشان دەدات. دوو تیر دەگەڕێنەوە بۆ دەستکاریکەرەکە: یەکێک لە javacـەوە بە نووسینی هەڵەکانی کاتی کۆمپایلکردن، یەکێک لە ئەنجامەکەوە بە نووسینی هەڵەکانی کاتی جێبەجێکردن یان ئەنجامی هەڵە.
The edit–compile–run cycleخولی دەستکاری — کۆمپایل — جێبەجێکردنCêIT

Mistakes send you back to the editor. A compile error means: fix the code and compile again. A runtime error or a wrong result means: change the code, compile it again and run it again.

هەڵەکان دەتگەڕێننەوە بۆ دەستکاریکەرەکە. واتە: کۆدەکە چاک بکە و دووپاتە کۆمپایل بکەرەوە. یان ئەنجامی هەڵە واتە: کۆدەکە بگۆڕە، دووپاتە کۆمپایلی بکەرەوە و دووپاتە جێبەجێی بکەرەوە.

Save the file with exactly the right nameفایلەکە ڕێک بە ناوی دروست پاشەکەوت بکە

Save the source code with the extension .java, under exactly the name of its public class: public class Welcome goes into Welcome.java — same letters, same capitals. Keep the programs of this track in one folder (for example examples), so the .class files appear next to their sources.

وەک فایلێکی .java پاشەکەوت بکە، ڕێک بە ناوی publicـەکەی: public class Welcome دەچێتە ناو Welcome.java — هەمان پیت و هەمان پیتە گەورەکان. ئەم ڕێڕەوە لە یەک هەڵبگرە (بۆ نموونە examples)، بۆ ئەوەی فایلەکانی .class لە تەنیشت فایلەکانی سەرچاوەیان دەربکەون.

For the code public class Welcome, four file names. Welcome.java is correct. welcome.java is wrong: javac says class Welcome is public, should be declared in a file named Welcome.java. Welcome.java.txt is wrong: the editor added a hidden .txt, so javac cannot find a .java file. Welcom.java is wrong: javac says file not found if you type the right name, or the class name does not match.بۆ کۆدی public class Welcome، چوار ناوی فایل. Welcome.java ڕاستە. welcome.java هەڵەیە: javac دەڵێت class Welcome is public, should be declared in a file named Welcome.java. Welcome.java.txt هەڵەیە: دەستکاریکەرەکە .txtـێکی شاراوەی زیاد کردووە، بۆیە javac فایلێکی .java نادۆزێتەوە. Welcom.java هەڵەیە: ناوی کلاسەکە و فایلەکە ناگونجێن.
One correct name and three common trapsیەک ناوی ڕاست و سێ داوی باوCêIT

Compile: javac makes bytecodeکۆمپایلکردن: javac بایتکۆد دروست دەکات

javac Welcome.java reads the source file, checks it against Java's rules and translates it into bytecode, which it writes into a new file, Welcome.class. Bytecode is like machine instructions, but for no real processor: it is architecture-neutral, so any computer with a Java Virtual Machine can run it. When javac prints nothing at all, the compile worked.

javac Welcome.java سەرچاوە دەخوێنێتەوە، بەراورد بە یاساکانی Java دەیپشکنێت و وەریدەگێڕێت بۆ ، کە لە فایلێکی نوێدا دەینووسێت: Welcome.class. وەک ئامێر وایە، بەڵام بۆ هیچ ڕاستەقینە نییە: سەربەخۆیە لە جۆری ئامێر، بۆیە هەر کە هەبێت دەتوانێت جێبەجێی بکات. کاتێک javac هیچ شتێک چاپ ناکات، کۆمپایلکردنەکە سەرکەوتوو بووە.

BASHType in a terminalلە تێرمیناڵدا بینووسە
javac Welcome.java
Animation: the file Welcome.java goes into javac and comes out as Welcome.class. The java command hands Welcome.class to the JVM, which also takes ready-made library classes such as System and String from java.base. The JVM runs the program and the console shows Welcome to Java!
Animation: the file Welcome.java goes into javac and comes out as Welcome.class. The java command hands Welcome.class to the JVM, which also takes ready-made library classes such as System and String from java.base. The JVM runs the program and the console shows Welcome to Java!
From source file to running programلە سەرچاوەوە بۆ کاراCêIT

If a class uses another class of yours, javac compiles that one too. Compiling Shop.java, which uses a class Tea from Tea.java, gives both Shop.class and Tea.class. Classes inside packages are compiled and run a little differently (Packages).

ئەگەر تری خۆت بەکار بهێنێت، javac ئەویش کۆمپایل دەکات. کۆمپایلکردنی Shop.java، کە Tea لە Tea.java بەکار دەهێنێت، هەم Shop.class و هەم Tea.class دەدات. ئەو لە ناو تۆزێک بە جیاوازی کۆمپایل و جێبەجێ دەکرێن (پاکێجەکان).

Run: java starts the JVMجێبەجێکردن: java ئامێری گریمانەیی Java دەست پێ دەکات

java Welcome starts a Java Virtual Machine, which loads Welcome.class and runs its main method. After java you type the class name: no .java and no .class. The same Welcome.class would run in the same way on any other computer with a JVM.

java Welcome دەست پێ دەکات، کە Welcome.class بار دەکات و جێبەجێ دەکات. دوای java ناوی دەنووسیت: بێ .java و بێ .class. هەمان Welcome.class بە هەمان شێوە لەسەر هەر تر کە JVMـی هەبێت جێبەجێ دەبێت.

BASHType in a terminalلە تێرمیناڵدا بینووسە
java Welcome
The command java Welcome with two labels: java starts the JVM; Welcome is the class name, without an extension. Below: java Welcome.class is wrong and ends with Could not find or load main class Welcome.class; java Welcome.java is a different command that compiles the source file in memory and runs it (Java 11 and later).فەرمانی java Welcome بە دوو ناونیشان: java ئامێری گریمانەیی Java دەست پێ دەکات؛ Welcome ناوی کلاسەکەیە، بێ پاشگری فایل. لە خوارەوە: java Welcome.class هەڵەیە و بە Could not find or load main class Welcome.class کۆتایی دێت؛ java Welcome.java فەرمانێکی ترە کە فایلی سەرچاوە لە بیرگەدا کۆمپایل دەکات و جێبەجێی دەکات (Java 11 و دواتر).
java takes a class name, not a file namejava ناوی وەردەگرێت، نەک ناوی CêIT

Adding .class is one of the most common first mistakes:

زیادکردنی .class یەکێکە لە باوترین یەکەم هەڵەکان:

BASHType in a terminalلە تێرمیناڵدا بینووسە
java Welcome.class

A whole session in the terminalدانیشتنێکی تەواو لە تێرمیناڵدا

Here is the whole cycle typed in a terminal on macOS or Linux. cd moves into the folder with the file (Folders and cd) and ls lists the files; on Windows' Command Prompt you type dir instead of ls.

ئەمە هەموو خولەکەیە کە لە macOS یان Linux نووسراوە. cd دەچێتە ناو ئەو تێدایە (فۆڵدەرەکان و cd) و ls لیستی پیشان دەدات؛ لە Command Promptـی Windowsـدا لە جیاتی ls دەنووسیت dir.

BASHType in a terminalلە تێرمیناڵدا بینووسە
cd examples
ls
javac Welcome.java
ls
java Welcome
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

No message from javac means success.

نەبوونی پەیام لە javacـەوە واتە سەرکەوتن.

Lineدێڕی 4

Now the folder also holds the bytecode file Welcome.class.

ئێستا فایلی ، Welcome.class، لەخۆ دەگرێت.

Animation of a terminal on the left and the examples folder on the right. The commands cd examples, ls, javac Welcome.java and java Welcome appear one at a time. The folder first shows only Welcome.java; after javac, Welcome.class appears beside it; after java Welcome, the terminal prints Welcome to Java!
Animation of a terminal on the left and the examples folder on the right. The commands cd examples, ls, javac Welcome.java and java Welcome appear one at a time. The folder first shows only Welcome.java; after javac, Welcome.class appears beside it; after java Welcome, the terminal prints Welcome to Java!
The folder after each command دوای هەر CêIT

On Windows the same commands work in Command Prompt, and javac and java print exactly the same messages:

لەسەر Windows هەمان لە Command Promptـدا کار دەکەن و javac و java ڕێک هەمان پەیام چاپ دەکەن:

BATType in a terminalلە تێرمیناڵدا بینووسە
cd C:\examples
dir
javac Welcome.java
java Welcome

Inside a .class fileناو فایلێکی .class

A .class file is not text. Every one starts with the same four bytes, CA FE BA BE — Java's "magic number" — followed by a version number: 00 3D is 61, the number for Java 17. The JDK tool javap -c shows the bytecode instructions in a readable form:

فایلێکی .class دەق نییە. هەموویان بە هەمان چوار بایت دەست پێ دەکەن، CA FE BA BE — «ژمارە سیحراوییەکەی» Java — و دوای ئەوان ژمارەی وەشانێک دێت: 00 3D دەکاتە ⁦61⁩، ژمارەی ⁦Java 17⁩. ئامرازی javap -cـی JDK بە شێوەیەکی خوێندراوە پیشان دەدات:

BASHType in a terminalلە تێرمیناڵدا بینووسە
javap -c Welcome
Three columns. Left: the source line System.out.println("Welcome to Java!");. Middle: its four bytecode instructions — getstatic gets System.out, ldc loads the text Welcome to Java!, invokevirtual calls println, return ends main. Right: the first bytes of Welcome.class, CA FE BA BE (the magic number) and 00 00 00 3D (version 61 = Java 17).سێ ستوون. چەپ: دێڕی سەرچاوەی System.out.println("Welcome to Java!");. ناوەڕاست: چوار ڕێکارنامەکەی بایتکۆد — getstatic System.out دەهێنێت، ldc دەقی Welcome to Java! بار دەکات، invokevirtual println بانگ دەکات، return main تەواو دەکات. ڕاست: یەکەم بایتەکانی Welcome.class، CA FE BA BE (ژمارە سیحراوییەکە) و 00 00 00 3D (وەشانی 61 = Java 17).
One line of Java and its bytecodeیەک دێڕی Java و CêIT

The main part is our one line of Java: getstatic fetches System.out, ldc loads the text, invokevirtual calls println, and return ends main. The first part, Welcome(), is a constructor that javac adds by itself (The default constructor). Here Welcome.java is 124 bytes and Welcome.class is 424 bytes. Because anyone can do this with a .class file, a compiled program is not a locked box: see Reverse Engineering.

بەشی main هەمان ئەو یەک دێڕەیە کە بە Java نووسیمان: getstatic System.out دەهێنێت، ldc دەقەکە بار دەکات، invokevirtual println بانگ دەکات و return main تەواو دەکات. بەشی یەکەم، Welcome()، کە javac خۆی زیادی دەکات (دروستکەری بنەڕەتی). لێرەدا Welcome.java ⁦124⁩ بایتە و Welcome.class ⁦424⁩ بایت. لەبەر ئەوەی هەر کەسێک دەتوانێت ئەمە لەگەڵ فایلێکی .class بکات، کۆمپایلکراو سندووقێکی قفڵدراو نییە: بڕوانە ئەندازیاریی پێچەوانە.

What the JVM does first: the class loader and the bytecode verifierیەکەم کارەکانی ئامێری گریمانەیی Java: بارکەری کلاس و پشکنەری بایتکۆد

When you type java Welcome, the JVM first uses a program called the class loader to find Welcome.class and load its bytecode into memory. Java's core classes, such as String and System, are already loaded while the JVM starts up. The program's other classes, such as your own, are loaded one by one, when they are first needed.

کاتێک java Welcome دەنووسیت، سەرەتا بەکار دەهێنێت بە ناوی بۆ دۆزینەوەی Welcome.class و بارکردنی بۆ ناو . Java، وەک String و System، لە کاتی دەستپێکردنی JVMـدا بار کراون. تری ، وەک هی خۆت، یەک بە یەک بار دەکرێن، کاتێک یەکەمجار پێویست دەبن.

Inside the JVM, from left to right: the class loader takes Welcome.class from the disk and System.class from the class library; the bytecode verifier checks the loaded bytecode — valid instructions, right types, no jumps out of a method — and stops a bad class with VerifyError; the execution engine with its interpreter and JIT compiler runs main and prints Welcome to Java!لە ناو ئامێری گریمانەیی Javaـدا، لە چەپەوە بۆ ڕاست: بارکەری کلاس Welcome.class لە دیسکەوە و System.class لە کتێبخانەی ستانداردەوە وەردەگرێت؛ پشکنەری بایتکۆد بایتکۆدی بارکراو دەپشکنێت — ڕێکارنامەکانی دروست، جۆری ڕاست، بێ بازدان بۆ دەرەوەی میتۆد — و کلاسێکی خراپ بە VerifyError ڕادەگرێت؛ بزوێنەری جێبەجێکردن بە ئینتەرپرێتەر و کۆمپایلەری JITـەوە main جێبەجێ دەکات و Welcome to Java! چاپ دەکات.
Load, verify, then runبارکردن، پشکنین، پاشان جێبەجێکردنCêIT

After a class is loaded, the JVM's bytecode verifier checks that the bytecode is valid and keeps to Java's safety rules: every instruction is real, values are used with the right types, and no jump leaves its method. A broken or tampered .class file is refused with a VerifyError before any of it runs. Only then does the JVM run main, interpreting the bytecode and compiling the busiest parts to machine code (just-in-time compiling).

دوای ئەوەی بار کرا، ـی JVM دەپشکنێت کە دروستە و یاساکانی سەلامەتیی Java دەپارێزێت: هەموو ڕاستەقینەیە، بە جۆری ڕاست بەکار دێن و هیچ بازدانێک لە خۆی دەرناچێت. فایلێکی .classـی تێکچوو یان دەستکاریکراو پێش ئەوەی هیچ بەشێکی جێبەجێ بێت بە VerifyError ڕەت دەکرێتەوە. تەنیا ئەو کاتە JVM main جێبەجێ دەکات، وەردەگێڕێت و بەشە زۆر بەکارهاتووەکان دەکات بە (کۆمپایلکردنی کاتی جێبەجێکردن).

What the JVM does first: the class loader and the bytecode verifierیەکەم کارەکانی ئامێری گریمانەیی Java: بارکەری کلاس و پشکنەری بایتکۆد

See a class being set up when it is neededببینە کلاسێک کاتی پێویستبوون ئامادە دەکرێت

You can watch this happen. The class Helper below prints a line at the moment the JVM sets it up (the static { … } block runs once, when the class is first used; you will meet static again later). Look at the order of the output:

دەتوانیت سەیری ڕوودانی ئەمە بکەیت. Helperـی خوارەوە لەو کاتەدا دێڕێک چاپ دەکات کە JVM ئامادەی دەکات (بلۆکی static { … } یەکجار جێبەجێ دەبێت، کاتێک یەکەمجار بەکار دێت؛ static دواتر دووپاتە دەبینیتەوە). سەیری ڕیزبەندیی بکە:

Lazy.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Lazy {
    public static void main(String[] args) {
        System.out.println("main starts");
        Helper.greet();
        System.out.println("main ends");
    }
}

class Helper {
    static {
        System.out.println("(the JVM sets up class Helper now)");
    }

    static void greet() {
        System.out.println("Hello from Helper");
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

The first use of Helper: only now is the class set up.

یەکەم بەکارهێنانی Helper: تەنیا ئێستا ئامادە دەکرێت.

main starts comes first: Helper was not needed before line 4. One file gave two class files, Lazy.class and Helper.class, and each is loaded only when it is needed — which makes Java programs start faster.

main starts یەکەمە: پێش دێڕی 4 پێویست بە Helper نەبوو. یەک فایل دوو فایلی دا: Lazy.class و Helper.class. هەریەکەیان تەنیا کاتێک بار دەکرێت کە پێویست بێت — ئەمەش وا دەکات Java خێراتر دەست پێ بکەن.

One click: running in an editorبە یەک کرتە: جێبەجێکردن لە دەستکاریکەرێکدا

Editors and IDEs run the same two steps for you. In VS Code, click Run above main (or Run Java at the top right of the editor); in IntelliJ IDEA, click the green arrow next to main. The output appears in a panel at the bottom. Behind the button, the editor compiles the file and starts a JVM — exactly what javac and java do.

دەستکاریکەرەکان و هەمان دوو هەنگاو بۆت ئەنجام دەدەن. لە VS Code، Run لە سەرووی main دابگرە (یان Run Java لە سەرەوەی لای ڕاستی دەستکاریکەرەکە)؛ لە IntelliJ IDEA، تیرە سەوزەکەی تەنیشت main دابگرە. لە بەشێکی خوارەوەدا دەردەکەوێت. لە پشت دوگمەکەوە، دەستکاریکەرەکە کۆمپایل دەکات و JVMـێک دەست پێ دەکات — ڕێک ئەوەی javac و java دەیکەن.

An editor window: Run | Debug appears above public static void main, and a Run panel at the bottom shows Welcome to Java!. A note beside the Run button says it runs javac Welcome.java and then java Welcome for you.پەنجەرەی دەستکاریکەرێک: Run | Debug لە سەرووی public static void main دەردەکەوێت و بەشی Run لە خوارەوە Welcome to Java! پیشان دەدات. تێبینییەک لە تەنیشت دوگمەی Run دەڵێت ئەو javac Welcome.java و پاشان java Welcome بۆت جێبەجێ دەکات.
The Run button = javac + javaدوگمەی ⁦Run = javac + java⁩CêIT
Running a program in TextPadجێبەجێکردنی بەرنامەیەک لە TextPadـدا
  1. Open TextPadTextPad بکەرەوە

    Some labs use TextPad, a paid editor for Windows only.

    هەندێک تاقیگە TextPad بەکار دەهێنن، دەستکاریکەرێکی پارەدار کە تەنیا بۆ Windowsـە.

  2. New fileفایلی نوێ

    Choose File → New, then Save.

    File → New هەڵبژێرە، پاشان Save.

  3. Save itپاشەکەوتی بکە

    Save it as HelloWorld.java in your own folder, for example on drive D:.

    بە ناوی HelloWorld.java لە خۆتدا پاشەکەوتی بکە، بۆ نموونە لەسەر درایڤی D:.

  4. Write the codeکۆدەکە بنووسە

    Type the HelloWorld program and save again.

    HelloWorld بنووسە و دووپاتە پاشەکەوت بکەرەوە.

  5. Compileکۆمپایل بکە

    Tools → External Tools → Compile Java, or Ctrl + 1 (this runs javac).

    Tools → External Tools → Compile Java، یان ⁦Ctrl + 1⁩ (ئەمە javac جێبەجێ دەکات).

  6. Runجێبەجێ بکە

    Tools → External Tools → Run Java Application, or Ctrl + 2 (this runs java).

    Tools → External Tools → Run Java Application، یان ⁦Ctrl + 2⁩ (ئەمە java جێبەجێ دەکات).

Shortcut: java Hello.javaکورتەڕێ: java Hello.java

Since JDK 11 the java launcher can also run a one-file program straight from its source code: it compiles the file in memory and runs it at once. No .class file is written.

لە ⁦JDK 11⁩ـەوە دەستپێکەری java دەتوانێت یەک فایلی ڕاستەوخۆ لە جێبەجێ بکات: لە کۆمپایل دەکات و یەکسەر جێبەجێی دەکات. هیچ فایلێکی .class نانووسرێت.

BASHType in a terminalلە تێرمیناڵدا بینووسە
java Hello.java
ls
Two ways side by side. Two steps: javac Hello.java writes Hello.class on the disk, then java Hello runs it — the usual way for programs with several files. One step (JDK 11 and later): java Hello.java compiles in memory and runs at once — no .class file, good for one-file programs and quick tests.دوو ڕێگا لە تەنیشت یەکەوە. دوو هەنگاو: javac Hello.java فایلی Hello.class لەسەر دیسک دەنووسێت، پاشان java Hello جێبەجێی دەکات — ڕێگای ئاسایی بۆ بەرنامەی چەند فایلی. یەک هەنگاو (JDK 11 و دواتر): java Hello.java لە بیرگەدا کۆمپایل دەکات و یەکسەر جێبەجێ دەکات — بێ فایلی .class، باشە بۆ بەرنامەی یەک فایلی و تاقیکردنەوەی خێرا.
Two steps or oneدوو هەنگاو یان یەکCêIT

The compile still happens, so a syntax error still stops the program: the launcher prints the javac message and then error: compilation failed. On JDK 17 a program made of several files must be compiled with javac (or a build tool); since JDK 22 the launcher can also run a program whose classes sit in several .java files.

کۆمپایلکردنەکە هەر ڕوو دەدات، بۆیە هێشتا ڕادەگرێت: دەستپێکەرەکە پەیامی javac و پاشان error: compilation failed چاپ دەکات. لەسەر ⁦JDK 17⁩، کە لە چەند فایلێک پێک دێت دەبێت بە javac (یان ئامرازێکی دروستکردن) کۆمپایل بکرێت؛ لە ⁦JDK 22⁩ـەوە دەستپێکەرەکە دەتوانێت ئەو جێبەجێ بکات کە لە چەند فایلێکی .javaـدان.

JShell: try one line at a timeJShell: هەر جارە یەک دێڕ تاقی بکەرەوە

To try a single line of Java without writing a class, start JShell with the command jshell (it comes with every JDK since 9). It reads one line, runs it and shows the result at once; results get names such as $2. Type /exit to leave.

بۆ تاقیکردنەوەی یەک دێڕی Java بەبێ نووسینی ، JShell بە jshell دەست پێ بکە (لە ⁦JDK 9⁩ـەوە لەگەڵ هەموو JDKـێک دێت). دێڕێک دەخوێنێتەوە، جێبەجێی دەکات و یەکسەر ئەنجامەکە پیشان دەدات؛ ئەنجامەکان ناوی وەک $2 وەردەگرن. بۆ دەرچوون /exit بنووسە.

BASHType in a terminalلە تێرمیناڵدا بینووسە
jshell
A loop of four steps around the jshell> prompt: you type a line; JShell reads it, runs it at once and prints the result, then shows the prompt again. Beside it: for whole programs, use a .java file with javac and java.خولێکی چوار هەنگاوی بە دەوری jshell>: دێڕێک دەنووسیت؛ JShell دەیخوێنێتەوە، یەکسەر جێبەجێی دەکات و ئەنجامەکە چاپ دەکات، پاشان دووپاتە jshell> پیشان دەدات. لە تەنیشتیدا: بۆ بەرنامەی تەواو، فایلێکی .java لەگەڵ javac و java بەکار بهێنە.
JShell: read, run, print, repeatJShell: خوێندنەوە، جێبەجێکردن، چاپکردن، دووپاتەCêIT

Common mistakesهەڵە باوەکان

Every message below is the real output of JDK 17. The first two rows are what Windows and macOS show, because their disks ignore capital letters in file names; on Linux javac and java simply find no such file or class. Reading a javac message explains the parts of a message.

هەموو پەیامێکی خوارەوە ڕاستەقینەی ⁦JDK 17⁩ـە. دوو ڕیزی یەکەم ئەوەن کە Windows و macOS پیشانی دەدەن، چونکە دیسکەکانیان گوێ بە پیتی گەورە لە ناوی فایلدا نادەن؛ لەسەر Linux، javac و java بە سادەیی ئەو یان نادۆزنەوە. خوێندنەوەی پەیامێکی javac بەشەکانی پەیامێک ڕوون دەکاتەوە.

You typedنووسیتYou seeدەبینیتFixچارەسەر
javac welcome.javaerror: class Welcome is public, should be declared in a file named Welcome.javaName the file exactly like the class: Welcome.java. ڕێک وەک ناو بنێ: Welcome.java.
java welcomeError: Could not find or load main class welcome Caused by: java.lang.NoClassDefFoundError: welcome (wrong name: Welcome)Type the class name with its capitals: java Welcome.ناوی بە پیتە گەورەکانیەوە بنووسە: java Welcome.
java Welcome.classError: Could not find or load main class Welcome.classLeave out .class..class لاببە.
javac Welcomeerror: Class names, 'Welcome', are only accepted if annotation processing is explicitly requestedjavac needs the file name with .java.javac ناوی لەگەڵ .java دەوێت.
javac Welcom.javaerror: file not found: Welcom.javaCheck the spelling, and cd into the folder that holds the file.ڕێنووسەکە بپشکنە و cd بکە بۆ ئەو تێدایە.
java WelcomeError: Could not find or load main class Welcome Caused by: java.lang.ClassNotFoundException: WelcomeYou are in another folder, or you have not compiled yet: run javac Welcome.java in the right folder first.لە تردایت، یان هێشتا کۆمپایلت نەکردووە: سەرەتا لە ڕاستدا javac Welcome.java جێبەجێ بکە.
javac (Windows)javac ⁦(Windows)⁩'javac' is not recognized as an internal or external commandThe JDK's bin folder is not in the PATH: see Check your JDK. binـی JDK لە PATHـدا نییە: بڕوانە کەرەستەی گەشەپێدانی Javaـەکەت بپشکنە.

Practiceڕاهێنان

ExerciseڕاهێنانHello, World! two ways⁦Hello, World!⁩ بە دوو ڕێگا

Write the HelloWorld program from Your first program, save it in a folder of its own and run it twice: (a) in a terminal with javac and java, (b) with the Run button of your editor. Check that both print the same line.

HelloWorld لە یەکەم بەرنامەکەت بنووسە، لە تایبەتدا پاشەکەوتی بکە و دوو جار جێبەجێی بکە: ⁦(a)⁩ لە بە javac و java، ⁦(b)⁩ بە دوگمەی Runـی دەستکاریکەرەکەت. دڵنیا ببەوە هەردووکیان هەمان دێڕ چاپ دەکەن.

Show answerوەڵامەکە پیشان بدە
BASHType in a terminalلە تێرمیناڵدا بینووسە
javac HelloWorld.java
java HelloWorld

The editor's Run button shows the same Hello, World! in its output panel, because it runs the same two steps.

دوگمەی Runـی دەستکاریکەرەکە هەمان Hello, World! لە بەشی پیشان دەدات، چونکە هەمان دوو هەنگاو جێبەجێ دەکات.

ExerciseڕاهێنانWhich commands fail?کام فەرمانانە شکست دەهێنن؟

Hello.java holds public class Hello. Which of these work, and what does each one that fails say? javac hello.java · javac Hello · javac Hello.java · java Hello.class · java Hello · java Hello.java

Hello.java public class Hello لەخۆ دەگرێت. کامیان کار دەکەن و ئەوانەی شکست دەهێنن چی دەڵێن؟ javac hello.java · javac Hello · javac Hello.java · java Hello.class · java Hello · java Hello.java

Show answerوەڵامەکە پیشان بدە
  • javac hello.java fails: the file is called Hello.java (on a disk that ignores case you get class Hello is public, should be declared in a file named Hello.java).
  • javac Hello fails: Class names, 'Hello', are only accepted if annotation processing is explicitly requested.
  • javac Hello.java works and writes Hello.class.
  • java Hello.class fails: Could not find or load main class Hello.class.
  • java Hello works (after javac).
  • java Hello.java works on JDK 11 and later, without javac.
  • javac hello.java شکست دەهێنێت: ناوی Hello.javaـە (لەسەر دیسکێک کە گوێ بە پیتی گەورە نادات class Hello is public, should be declared in a file named Hello.java دەبینیت).
  • javac Hello شکست دەهێنێت: Class names, 'Hello', are only accepted if annotation processing is explicitly requested.
  • javac Hello.java کار دەکات و Hello.class دەنووسێت.
  • java Hello.class شکست دەهێنێت: Could not find or load main class Hello.class.
  • java Hello کار دەکات (دوای javac).
  • java Hello.java لەسەر ⁦JDK 11⁩ و دواتر کار دەکات، بەبێ javac.
ExerciseڕاهێنانLook insideسەیری ناوەوە بکە

Compile your HelloWorld and run javap -c HelloWorld. Find the line that loads your text with ldc. Then look at the first bytes of HelloWorld.class (xxd HelloWorld.class | head -1 on macOS or Linux, Format-Hex HelloWorld.class in Windows PowerShell).

HelloWorldـەکەت کۆمپایل بکە و javap -c HelloWorld جێبەجێ بکە. ئەو دێڕە بدۆزەرەوە کە بە ldc دەقەکەت بار دەکات. پاشان سەیری یەکەم بایتەکانی HelloWorld.class بکە (xxd HelloWorld.class | head -1 لەسەر macOS یان Linux، Format-Hex HelloWorld.class لە PowerShellـی Windows).

Show answerوەڵامەکە پیشان بدە

In main you find ldc … // String Hello, World! between getstatic (System.out) and invokevirtual (println). The file starts with cafe babe 0000 003d on JDK 17: the magic number, then version 61.

لە mainـدا ldc … // String Hello, World! لە نێوان getstatic ⁦(System.out)⁩ و invokevirtual ⁦(println)⁩ دەدۆزیتەوە. لەسەر ⁦JDK 17⁩ بە cafe babe 0000 003d دەست پێ دەکات: ژمارە سیحراوییەکە، پاشان ⁦61⁩.

ExerciseڕاهێنانOne step or two?یەک هەنگاو یان دوو؟

In an empty folder, run java HelloWorld.java, then list the folder. Explain why there is no HelloWorld.class. Then run javac HelloWorld.java and list the folder again.

لە بەتاڵدا java HelloWorld.java جێبەجێ بکە، پاشان لیست بکە. ڕوونی بکەرەوە بۆچی HelloWorld.class نییە. پاشان javac HelloWorld.java جێبەجێ بکە و دووپاتە لیست بکە.

Show answerوەڵامەکە پیشان بدە

The launcher compiled the program in memory, ran it and threw the bytecode away, so only HelloWorld.java is in the folder. After javac HelloWorld.java the folder also holds HelloWorld.class.

دەستپێکەرەکە لە کۆمپایل کرد، جێبەجێی کرد و فڕێ دا، بۆیە تەنیا HelloWorld.java لە . دوای javac HelloWorld.java، HelloWorld.classـیش لەخۆ دەگرێت.

Summaryپوختە

  • Write → save as ClassName.java → javac ClassName.java → java ClassName; go back to the editor after any error.
  • javac checks and translates source code into architecture-neutral bytecode in a .class file, which starts with CA FE BA BE.
  • java starts the JVM: the class loader loads classes when they are first needed, the bytecode verifier checks them, then the JVM interprets and JIT-compiles the code.
  • After java comes the class name, without .class; file and class names must match exactly, capitals included.
  • IDE Run buttons and TextPad's Ctrl + 1 / Ctrl + 2 run javac and java for you; java Hello.java runs a one-file program without javac; jshell tries one line at a time.
  • بنووسە ← بە ClassName.java پاشەکەوتی بکە ← javac ClassName.java ← java ClassName؛ دوای هەر هەڵەیەک بگەڕێوە بۆ دەستکاریکەرەکە.
  • javac دەپشکنێت و وەریدەگێڕێت بۆ سەربەخۆ لە جۆری ئامێر لە فایلێکی .classـدا، کە بە CA FE BA BE دەست پێ دەکات.
  • java دەست پێ دەکات: کاتێک یەکەمجار پێویست بن بار دەکات، دەیانپشکنێت، پاشان JVM کۆدەکە وەردەگێڕێت و بە JIT کۆمپایلی دەکات.
  • دوای java ناوی دێت، بێ .class؛ ناوی و دەبێت ڕێک وەک یەک بن، پیتە گەورەکانیشەوە.
  • دوگمەکانی Runـی و ⁦Ctrl + 1 / Ctrl + 2⁩ـی TextPad javac و java بۆت جێبەجێ دەکەن؛ java Hello.java یەک فایلی بەبێ javac جێبەجێ دەکات؛ jshell هەر جارە یەک دێڕ تاقی دەکاتەوە.
Review questionsپرسیارەکانی پێداچوونەوە
Why must you compile again after every change?بۆچی دەبێت دوای هەموو گۆڕانکارییەک دووپاتە کۆمپایل بکەیتەوە؟

java runs the bytecode in the .class file; only javac updates it from your source.

java ئەو جێبەجێ دەکات کە لە فایلی .classـدایە؛ تەنیا javac لە نوێی دەکاتەوە.

What is the difference between the class loader and the bytecode verifier?جیاوازیی نێوان بارکەری کلاس و پشکنەری بایتکۆد چییە؟

The class loader finds .class files and loads them into memory when they are needed; the verifier then checks that the loaded bytecode is valid and safe.

فایلەکانی .class دەدۆزێتەوە و لە کاتی پێویستدا باریان دەکاتە ناو ؛ پاشان دەپشکنێت کە بارکراو دروست و سەلامەتە.

When is java Hello.java enough?کەی java Hello.java بەسە؟

For a program in one file, on JDK 11 or newer. On JDK 17, programs with several files are compiled with javac; JDK 22 and later can launch them from source too.

بۆ لە یەک فایلدا، لەسەر ⁦JDK 11⁩ یان نوێتر. لەسەر ⁦JDK 17⁩، چەند فایلی بە javac کۆمپایل دەکرێت؛ ⁦JDK 22⁩ و دواتر دەتوانن ئەوانیش ڕاستەوخۆ لە جێبەجێ بکەن.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Errors and Debugging: Syntax, Runtime and Logic Errorsهەڵەکان و کرمەکوژی: هەڵەی داڕستن، هەڵەی کاتی جێبەجێکردن و هەڵەی لۆجیکی

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.