ရိုးရိုးလေး ရှင်းပြမယ်
Setup ကို ရိုးရိုးလေး ပြောရရင်
Kotlin code ကို run ဖို့ အဲဒီ code ကို compile လုပ်ပေးမယ့် အရာတစ်ခု လိုပါတယ်။ လမ်းသုံးလမ်း ရှိပါတယ်။ Browser ထဲက Playground ကတော့ ဘာမှ install မလုပ်ရဘဲ စမ်းလို့ရပါတယ်။ IDE (IntelliJ IDEA ဒါမှမဟုတ် Android Studio) က project အပြည့်အစုံအတွက် အလုပ်ရုံတစ်ခုလို အားလုံးပါပါတယ်။ Command line မှာတော့ JDK နဲ့ `kotlinc` ကို install လုပ်ပြီး ကိုယ်တိုင် compile လုပ်ရပါတယ်။
အခုအဆင့်မှာ တစ်ခုပဲ ရွေးရင် လုံလောက်ပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ လမ်းသုံးလမ်းလုံးက code အတူတူကို run ပါတယ်။ ဒါပေမယ့် command line ကို တစ်ခါလောက် စမ်းကြည့်ထားရင် IDE က နောက်ကွယ်မှာ ဘာလုပ်ပေးနေလဲ နားလည်သွားပါလိမ့်မယ်။
Problem → Why → Solution
ပြဿနာ: code မှန်ပေမယ့် run လို့မရ
Lesson ထဲက program တစ်ခုကို အတိအကျ copy လုပ်ပြီး `hello.kt` အဖြစ် သိမ်းကာ compile လုပ်ကြည့်တဲ့အခါ terminal က `kotlinc` ကို မသိဘူးလို့ ပြောပါတယ်။ တစ်ခုချင်း ပြင်ပြီး compile ဖြစ်သွားပေမယ့် jar ကို run လိုက်တဲ့အခါ `NoClassDefFoundError` နဲ့ crash ဖြစ်ပါတယ်။ Error တွေကို မမြင်ဖူးသေးရင် code မှားနေတယ်လို့ ထင်ပြီး မှန်နေတဲ့ code ကို ပြင်နေမိတတ်ပါတယ်။
$ kotlinc hello.kt -include-runtime -d hello.jar
bash: kotlinc: command not found
# later, after installing, but compiling without -include-runtime:
$ kotlinc hello.kt -d hello.jar
$ java -jar hello.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsicsဘာကြောင့် ဒီ error တွေ ဖြစ်လဲ
နှစ်ခုလုံးက code နဲ့ မဆိုင်ဘဲ environment နဲ့ ဆိုင်ပါတယ်။ Shell က command ကို PATH ထဲ ဖော်ပြထားတဲ့ folder တွေထဲမှာပဲ ရှာတာမို့ `kotlinc` ရှိတဲ့ folder က PATH ထဲ မပါရင် ရှာမတွေ့ပါဘူး။ ဒုတိယ error ကတော့ `-include-runtime` မပါဘဲ ထုတ်တဲ့ jar ထဲမှာ Kotlin standard library class တွေ မပါလို့ ဖြစ်ပါတယ်။
အဓိက သဘောတရား: source ကနေ jar ကို ဖြတ်ပြီး JVM အထိ
အရင်ဆုံး ကိုယ့်အခြေအနေနဲ့ ကိုက်တဲ့ လမ်းကို ရွေးပါ။ အောက်က table က တစ်ခုချင်းစီ ဘာအတွက် သင့်တော်လဲ ပြထားပါတယ်။ ဒီ course ရဲ့ example တိုင်းက `fun main()` ပါတဲ့ file တစ်ခုတည်းမို့ လမ်းသုံးလမ်းလုံးမှာ အလုပ်လုပ်ပါတယ်။
| လမ်း | ဘာလိုလဲ | အကောင်းဆုံး အသုံးဝင်ပုံ |
|---|---|---|
| Kotlin Playground | Browser ပဲ (play.kotlinlang.org) | Example တွေကို မြန်မြန် စမ်းကြည့်ဖို့ |
| IntelliJ IDEA / Android Studio | IDE တစ်ခု download လုပ်ရန် | Project အစစ်၊ debugging၊ refactoring |
| Command line | JDK နဲ့ kotlinc | Compiler ဘာလုပ်လဲ နားလည်ဖို့၊ script နဲ့ CI |
Command-line လမ်းကို အဆင့်လိုက် သွားပါမယ်။ အဆင့်တစ်ခုချင်းရဲ့ ရလဒ်ကို စစ်ပြီးမှ နောက်တစ်ဆင့် သွားရင် ပြဿနာပေါ်တဲ့အခါ ဘယ်အဆင့်မှာလဲ ချက်ချင်း သိနိုင်ပါတယ်။
1. JDK ကို install လုပ်ပြီး စစ်ပါ
Kotlin compiler နဲ့ ထွက်လာတဲ့ program နှစ်ခုလုံး JVM ပေါ်မှာ run တာမို့ JDK (Java Development Kit) လိုပါတယ်။ Terminal ကို ဖွင့်ပြီး `java -version` ကို run ပါ။ Version နံပါတ်ပါတဲ့ စာတစ်ကြောင်း ပေါ်ရပါမယ်။
2. kotlinc ကို install လုပ်ပါ
SDKMAN နဲ့ `sdk install kotlin`၊ Homebrew နဲ့ `brew install kotlin` ဒါမှမဟုတ် Kotlin GitHub releases စာမျက်နှာကနေ compiler zip ကို download လုပ်ပြီး unzip လုပ်ကာ `bin` folder ကို PATH ထဲ ထည့်ပါ။
3. Terminal အသစ်မှာ စစ်ပါ
Terminal အသစ် ဖွင့်ပြီး `kotlinc -version` ကို run ပါ။ `info: kotlinc-jvm 2.x.x ...` လို စာတစ်ကြောင်း ပေါ်ရပါမယ်။ PATH ကို ပြောင်းပြီးတဲ့နောက် terminal အသစ်မှာပဲ အလုပ်လုပ်ပါတယ်။
4. Compile လုပ်ပြီး run ပါ
`hello.kt` ကို သိမ်းပြီး `kotlinc hello.kt -include-runtime -d hello.jar` နဲ့ compile လုပ်ပါ။ ပြီးရင် `java -jar hello.jar` နဲ့ run ပါ။ Compiler က စက္ကန့်အနည်းငယ် ကြာတတ်ပါတယ်၊ ပုံမှန်ပါပဲ။
Compile command ရဲ့ အစိတ်အပိုင်းတွေကို ရှင်းပြပါမယ်။ `-d hello.jar` က output ကို jar file အဖြစ် နာမည်ပေးတာပါ။ `-include-runtime` က Kotlin standard library ကို jar ထဲ ကူးထည့်ပေးလို့ jar တစ်ခုတည်းနဲ့ JDK ရှိတဲ့ စက်ပေါ်မှာ run လို့ရပါတယ်။ `hello.kt` ထဲက top-level `main` function က `HelloKt` ဆိုတဲ့ class ထဲ ရောက်ပြီး compiler က jar ရဲ့ manifest ထဲမှာ ဒီ class ကို main class အဖြစ် မှတ်ပေးပါတယ်၊ ဒါကြောင့် `java -jar` က ဘာ class ကို စရမလဲ သိပါတယ်။
$ java -version
$ kotlinc -version
$ kotlinc hello.kt -include-runtime -d hello.jar
$ java -jar hello.jar- hello.kt: ကိုယ့် source
- kotlinc: စစ်ပြီး compile
- hello.jar: bytecode နဲ့ runtime
- java -jar: JVM က run
Error တစ်ခု ပေါ်ရင် ဘယ်မှာ ရပ်သွားလဲ ကြည့်ပါ — `kotlinc` မှာလား၊ `java` မှာလား။ ပြဿနာရဲ့ ထက်ဝက်ကို အဲဒါနဲ့ ခွဲလို့ရပါတယ်။
အထွေထွေ ပုံစံ: command line workflow
java -version
kotlinc -version
kotlinc SourceFile.kt -include-runtime -d name.jar
java -jar name.jarအပေါ်နှစ်ကြောင်းက tool တွေ ရှိမရှိ စစ်ပါတယ်။ တတိယ line က `.kt` file ကို jar အဖြစ် compile လုပ်ပြီး နောက်ဆုံး line က အဲဒီ jar ကို JVM နဲ့ run ပါတယ်။ File နာမည်နဲ့ jar နာမည်ကို ကိုယ်ကြိုက်သလို ပြောင်းလို့ရပါတယ်။
Setup ကို စစ်ဆေးပေးတဲ့ program
`hello.kt` ထဲမှာ ဒီ program ကို ရေးပြီး အပေါ်က command လေးကြောင်းနဲ့ run ကြည့်ပါ။ Playground မှာဆိုရင် paste လုပ်ပြီး Run ကို နှိပ်ပါ။ Kotlin 2.x ကို သုံးနေရင် output က အောက်ပါအတိုင်း ထွက်ရပါမယ်၊ ဒီထက် နောက်ကျတဲ့ major version ကို သုံးရင် ပထမ line က ပြောင်းသွားပါမယ်။
fun main() {
val kotlin = KotlinVersion.CURRENT
println("Kotlin major version: ${kotlin.major}")
println("Kotlin 2 or newer: ${kotlin.isAtLeast(2, 0)}")
println("Running on a JVM: ${System.getProperty("java.version") != null}")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The running Kotlin version
val kotlin = KotlinVersion.CURRENT`KotlinVersion.CURRENT` က standard library ကနေ ပေးတဲ့ object တစ်ခုပါ၊ အခု run နေတဲ့ Kotlin ရဲ့ major, minor, patch နံပါတ်တွေ ပါပါတယ်။ ဒီ line က value ကို `kotlin` ဆိုတဲ့ နာမည်နဲ့ သိမ်းထားတာပါ။
- Reading one property
println("Kotlin major version: ${kotlin.major}")`kotlin.major` က major version နံပါတ်ကို ပြန်ပေးပါတယ်။ `.` ပါတဲ့ expression ဖြစ်လို့ string template ထဲမှာ ကွင်းကောက် `${ }` နဲ့ ထည့်ရပါတယ်။
- A Boolean check
println("Kotlin 2 or newer: ${kotlin.isAtLeast(2, 0)}")`isAtLeast(2, 0)` က version က 2.0 နဲ့ ညီ ဒါမှမဟုတ် ပိုမြင့်ရင် `true` ပြန်ပါတယ်။ ဒီ course က Kotlin 2.x နဲ့ ရေးထားလို့ သင့်စက်မှာ `true` ထွက်သင့်ပါတယ်။
- Calling a Java API
System.getProperty("java.version")`System` က Java ရဲ့ class ပါ၊ Kotlin ကနေ import မလုပ်ဘဲ တိုက်ရိုက် ခေါ်လို့ရပါတယ်။ `!= null` နဲ့ ယှဉ်လိုက်တဲ့အတွက် JVM ပေါ်မှာ run နေတယ်ဆိုတာ `true` / `false` အဖြစ် ရပါတယ်။
Kotlin major version: 2
Kotlin 2 or newer: true
Running on a JVM: true- hello.kt ကို သိမ်းတယ်
- kotlinc က စစ်ပြီး compile လုပ်တယ်
- hello.jar ကို ရေးတယ်
- java -jar က run ပြီး print ထုတ်တယ်
Compile ဖြစ်တယ်
kotlinc finishes silently and hello.jar appears in the folder
Compile မဖြစ်ဘူး
Error lines with file, line and column; no jar is written
Gradle project ရဲ့ ပုံစံ
Project အစစ်တွေမှာ `kotlinc` ကို လက်နဲ့ မခေါ်ကြပါဘူး။ Gradle ဆိုတဲ့ build tool က compile၊ dependency နဲ့ run ကို စီမံပါတယ်။ IDE ရဲ့ New Project wizard က အောက်ပါလို `build.gradle.kts` file ကို ကိုယ့်အတွက် ဖန်တီးပေးပါတယ်။ Source code တွေကို `src/main/kotlin` folder ထဲ ထားပြီး `src/main/kotlin/Main.kt` ထဲက `main` ကို `MainKt` class အဖြစ် ရှာတွေ့ပါတယ်။
plugins {
kotlin("jvm") version "2.1.21"
application
}
repositories {
mavenCentral()
}
kotlin {
jvmToolchain(21)
}
application {
mainClass.set("MainKt")
}Wizard က ထည့်ပေးတဲ့ version နံပါတ်ကို အဲဒီအတိုင်း ထားပါ၊ အပေါ်က 2.1.21 က ဥပမာတစ်ခုပါ။ Terminal ကနေ project ကို run ချင်ရင် project folder ထဲမှာ `./gradlew run` ကို သုံးပါတယ် (Windows မှာ `gradlew run`)။ Gradle ကို နောက်ပိုင်း lesson တစ်ခုမှာ အသေးစိတ် လေ့လာပါမယ်။
ပထမနေ့ error တွေကို ရှာဖွေဖြေရှင်းခြင်း
| Message | အကြောင်းရင်း | ဖြေရှင်းနည်း |
|---|---|---|
| kotlinc: command not found | bin folder က PATH ထဲ မပါဘူး | PATH ထဲ ထည့်ပြီး terminal အသစ် ဖွင့်ပါ |
| error: source file or directory not found: hello.kt | Folder မှားနေတယ် | `cd` နဲ့ file ရှိတဲ့ folder ထဲ ဝင်ပါ |
| NoClassDefFoundError: kotlin/jvm/internal/Intrinsics | -include-runtime မပါဘဲ build ထားတယ် | Flag ထည့်ပြီး ပြန် compile |
| Error: Could not find or load main class hello.jar | java ကို -jar မပါဘဲ ခေါ်တယ် | `java -jar hello.jar` လို့ ရေးပါ |
လက်တွေ့ project တွေမှာ
လက်တွေ့ team တွေမှာ setup ကို ဘယ်လို လုပ်လဲ
Team project တွေမှာ ဘယ်သူမှ `kotlinc` ကို လက်နဲ့ မရိုက်ကြပါဘူး။ Repository ကို clone လုပ်ပြီး `./gradlew build` run ရုံနဲ့ build ဖြစ်ရပါမယ်။ Kotlin version နဲ့ JDK version ကို `build.gradle.kts` ထဲမှာ ကြေညာထားလို့ developer အားလုံးရော CI server ပါ တူညီတဲ့ tool တွေနဲ့ build ဖြစ်ပါတယ်။ ဒါကြောင့် 'ငါ့စက်မှာတော့ run တယ်' ဆိုတဲ့ ပြဿနာ နည်းသွားပါတယ်။
ဒါပေမယ့် ဒီ lesson ရဲ့ kotlinc → jar → java လမ်းကြောင်းကို နားလည်ထားတာက အရေးကြီးပါတယ်။ Build တစ်ခု မမျှော်လင့်ဘဲ fail ဖြစ်တဲ့အခါ Gradle က နောက်ကွယ်မှာ အဲဒီ အဆင့်တွေပဲ run နေတာမို့ ဘယ်အဆင့်မှာ ပြဿနာရှိလဲ ခွဲစစ်နိုင်ပါတယ်။ Docker image ထဲ Kotlin app ထည့်တဲ့အခါလည်း JDK တစ်ခု နဲ့ jar တစ်ခုပဲ လိုတာကို မြင်ရပါလိမ့်မယ်။
အမှားများ
Setup မှာ အဖြစ်များတဲ့ အမှားတွေ
1. PATH ပြောင်းပြီး terminal အဟောင်းကို ဆက်သုံးတာ
Install လုပ်ပြီးနောက်မှာလည်း `kotlinc: command not found` ဆက်ပေါ်တာမျိုးပါ။ ဖွင့်ထားပြီးသား terminal က PATH အဟောင်းကို ဆက်သုံးနေလို့ပါ။ Terminal ကို ပိတ်ပြီး အသစ်ဖွင့်ပါ၊ VS Code ထဲက terminal ဆိုရင် VS Code ကိုပါ ပြန်ဖွင့်ပါ။
2. `-include-runtime` မေ့တာ
Program က `println` လောက်ပဲ သုံးရင် runtime မပါတဲ့ jar တောင် run နေတတ်လို့ ပြဿနာကို မသိလိုက်ပါဘူး။ Parameter ပါတဲ့ function ဒါမှမဟုတ် list လို library type တွေ သုံးတဲ့အခါမှ `NoClassDefFoundError` ပေါ်ပါတယ်။ Jar ကို တခြားသူဆီ ပို့ရင်လည်း ဒီအမှားက ပေါ်လာပါမယ်၊ ဒါကြောင့် အမြဲ flag ထည့်ပါ။
3. Run command ကို မှားရိုက်တာ
`java hello.jar` လို့ ရိုက်ရင် `Error: Could not find or load main class hello.jar` ပေါ်ပါတယ်၊ `-jar` မပါလို့ java က `hello.jar` ကို class နာမည်လို့ ယူလိုက်တာပါ။ `java -jar hello.jar` လို့ ရေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Exercise တစ်ခုစီအတွက် folder တစ်ခုစီ ဖန်တီးပါ၊ file တွေ ရောမသွားအောင်ပါ။
- Playground ကို အမြန်ကြည့်ဖို့ သုံးပြီး ဆယ်ကြောင်းထက်ပိုတဲ့ code ကို IDE ထဲ ပြောင်းရေးပါ။
- Install လုပ်ပြီးတိုင်း `java -version` နဲ့ `kotlinc -version` ကို အရင်စစ်ပါ၊ lesson ထဲက error လို့ ထင်မိတာတွေ တော်တော်များများကို ရှောင်နိုင်ပါတယ်။
Mini challenge
Mini challenge: setup report
ကိုယ့်စက်ရဲ့ Kotlin version၊ Java version နဲ့ CPU core အရေအတွက်ကို print ထုတ်တဲ့ `Report.kt` ကို ရေးပြီး jar အဖြစ် compile လုပ်ကာ run ပါ။
- `Setup report` ဆိုတဲ့ ခေါင်းစဉ်တစ်ကြောင်း print ထုတ်ပါ
- `KotlinVersion.CURRENT` နဲ့ Kotlin version ကို print ထုတ်ပါ
- `System.getProperty("java.version")` နဲ့ Java version ကို print ထုတ်ပါ
- `Runtime.getRuntime().availableProcessors()` နဲ့ core အရေအတွက်ကို print ထုတ်ပြီး `-include-runtime` ပါတဲ့ command နဲ့ jar ထုတ်ကာ run ပါ
fun main() {
// TODO: print the heading "Setup report"
// TODO: print the Kotlin version (KotlinVersion.CURRENT)
// TODO: print the Java version (System.getProperty with "java.version")
// TODO: print how many CPU cores are available
}ရလဒ်: စက်ပေါ် မူတည်ပြီး ကွဲပါမယ်၊ ဥပမာ: Setup report Kotlin: 2.1.21 Java: 21.0.13 CPU cores: 8
အရိပ်အမြွက် ကြည့်ရန်
`println("Kotlin: ${KotlinVersion.CURRENT}")` ပုံစံနဲ့ template ထဲ ထည့်ပါ။ Java ရဲ့ `Runtime.getRuntime()` ကို Kotlin ကနေ တိုက်ရိုက်ခေါ်လို့ရပြီး `availableProcessors()` က Int ပြန်ပါတယ်။
အဖြေ ကြည့်ရန်
fun main() {
println("Setup report")
println("Kotlin: ${KotlinVersion.CURRENT}")
println("Java: ${System.getProperty("java.version")}")
println("CPU cores: ${Runtime.getRuntime().availableProcessors()}")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Playground၊ IDE နဲ့ command line သုံးလမ်းလုံးက code အတူတူကို run ပါတယ်၊ ကိုယ့်အခြေအနေနဲ့ ကိုက်တာကို ရွေးပါ။
- Command line မှာ JDK နဲ့ kotlinc လိုပြီး `java -version` နဲ့ `kotlinc -version` ကို terminal အသစ်မှာ စစ်ပါ။
- `kotlinc hello.kt -include-runtime -d hello.jar` နဲ့ compile လုပ်ပြီး `java -jar hello.jar` နဲ့ run ပါ။
- `command not found` က PATH ပြဿနာ၊ `NoClassDefFoundError` က runtime မပါတဲ့ jar ပြဿနာပါ။
Kotlin releases — Kotlin