https://kotlinlang.org logo
Title
v

v79

06/29/2022, 8:04 PM
I'm mighty confused. I've got a Kotlin 1.7 project, requiring JDK 18. It doesn't compile:
e: D:\Development\Experiments\Pellet\server\src\main\kotlin\dev\pellet\server\PelletServerClient.kt: (8, 17): Unresolved reference: UnixDomainSocketAddress
IntelliJ can see the import (no red squiggly lines). In Project Structure, I've chosen SDK `openjdk-18 java version "18.0.1"`(and I've also tried the cornetto variant of JDK18). The language level is set to 17 in InteilliJ and in the
build.gradle.kts
file. In IntelliJ's External Libraries view I can drill down to
<http://java.base.java.net|java.base.java.net>
and see the
UnixDomainSocketAddress
class (it was introduced in Java 16). But neither IntelliJ nor Gradle can find the class to compile it! (I've even dug into the JDK source folder, and it definitely exists!). Please, any ideas?
Eventually I tried adding some really direct statements into build.gradle.kts:
kotlin {
        jvmToolchain {
         languageVersion.set(JavaLanguageVersion.of("17"))
        }
    }
And it worked. So then it dawned on me that JAVA_HOME environment variable might be wrong - and it was pointing to a JDK11 installation. So all that time I've been writing Kotlin code targeting JDK15, I've never once used a class which didn't exist and work in JDK11.
Never have I had to deal with an issue like this. I've learned my lesson.
k

Klitos Kyriacou

06/30/2022, 9:49 AM
I don't know why most developers define JAVA_HOME. If you don't define it, build tools will use the version of Java that's in your PATH, which is usually what you want.
v

v79

06/30/2022, 10:15 AM
I'll certainly try that, thanks