5 min,  Kotlin

5 min Kotlin and AdopOpenJDK and make jar (Extended import java.util.*)

Files after download

Download AdoptOpenJDK


AdoptOpenJDK – Open source, prebuilt OpenJDK binaries


OpenJDK11U-jdk_x64_windows_hotspot_11.0.13_8.msi

On install:
Add to PATH, Set JAVA_HOME on local HD.

C:\Users\jekl>java -version
openjdk version “11.0.13” 2021-10-19
OpenJDK Runtime Environment Temurin-11.0.13+8 (build 11.0.13+8)
OpenJDK 64-Bit Server VM Temurin-11.0.13+8 (build 11.0.13+8, mixed mode

Download Kotlin zip


Release Kotlin 1.6.10 · JetBrains/kotlin (github.com)


kotlin-compiler-1.6.10.zip unzip it somewhere

C:\giti2021\Kotlins\kotlin-compiler-1.6.10\kotlinc

Add KOTLIN_HOME and Path


Click on the Edit environment variables for your account shortcut.


Enter “KOTLIN_HOME” as variable name. Enter the [KOTLIN_INSTALL_DIR] as variable value.


Next, we need to configure the PATH environment variable so we can run Kotlin from a command prompt.

Select the PATH variable. Click on Edit, New and add a new path:
%KOTLIN_HOME%\bin

C:\Users\jekl>kotlinc -version
info: kotlinc-jvm 1.6.10 (JRE 11.0.13+8)

Verify home with cmd

echo %JAVA_HOME%
echo %KOTLIN_HOME%

Yields
C:\Program Files\Eclipse Adoptium\jdk-11.0.13.8-hotspot\
C:\giti2021\Kotlins\kotlin-compiler-1.6.10\kotlinc

Consider project files

Where Main.kt is importing both classes

Compile and run a jar (single class)

# Compile from .
kotlinc -include-runtime -d Main.jar Main.kt
# Run it
java -jar Main.jar

Compile and run a jar (multiple class, will work here since main is referencing 2 classes)

# Compile from .
kotlinc -include-runtime -d Main.jar Main.kt Helper.kt Utility.kt
# Run it
java -jar Main.jar

Result


Note, dependency and red lines in VSC

vscode: cannot access built in declaration. · Issue #153 · fwcd/kotlin-language-server (github.com)

Turns out as soon as I installed gradle, using sdk install gradle, language-server suddenly started functioning as expected. Without adding a gradle project in the directory.

Install Gradle (did not help, but could be nice to learn Gradle)

Gradle | Installation

Microsoft Windows users
Create a new directory C:\Gradle with File Explorer.

Open a second File Explorer window and go to the directory where the Gradle distribution was downloaded. Double-click the ZIP archive to expose the content. Drag the content folder gradle-7.3.2 to your newly created C:\Gradle folder.

Alternatively you can unpack the Gradle distribution ZIP into C:\Gradle using an archiver tool of your choice.

Step 3. Configure your system environment

Microsoft Windows users
In File Explorer right-click on the This PC (or Computer) icon, then click Properties -> Advanced System Settings -> Environmental Variables.

Under System Variables select Path, then click Edit. Add an entry for C:\Gradle\gradle-7.3.2\bin. Click OK to save.

Step 4. Verify your installation
Open a console (or a Windows command prompt) and run gradle -v to run gradle and display the version, e.g.:

Still :

visual studio code – Kotlin, unresolved reference on everything in VSCode – Stack Overflow

Remove Kotlin, but keep kotlin language (did help)

Import java.util.*

Since we are using the JVM from AdoptOpenJDK we can use all libs fra Java.

import java.util.*

class Utility {

    fun getStr(): String {
        return "Utility str"
    }
    
    fun randomJava() {
        var ran = Random()
        var nr  = ran.nextInt(100)
        var fl  = ran.nextFloat()
        println(nr)
        println(fl)
    }  
}

Compile it and run it:

kotlinc -include-runtime -d Main.jar Main.kt Helper.kt Utility.kt
java -jar Main.jar

[]
Hello from VSC
12
WORKER1 28
Utility str
30
0.27628368
Comments Off on 5 min Kotlin and AdopOpenJDK and make jar (Extended import java.util.*)