我最近开始用Kotlin编写一个Spring Boot应用程序.我打算用柯特琳·科鲁廷.我已经在Gradle文件中添加了所有必需的依赖项,但IDE仍然显示以下错误:

Cannot access class 'kotlin.coroutines.CoroutineContext'. Check your module classpath for missing or conflicting dependencies

以下是我编写的示例代码:

fun createThread(): MyThread {
    GlobalScope.async {
        openApiService.createThread()
    }.await()

}

另外,以下是My Gradle文件:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.2.1"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.21"
    kotlin("plugin.spring") version "1.9.21"
}

group = "<mypackage>"
version = "0.0.1-SNAPSHOT"

java {
    sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    implementation("com.aallam.openai:openai-client:3.6.2")
    implementation("io.ktor:ktor-client-java:2.3.2")
    implementation("com.theokanning.openai-gpt3-java:service:0.18.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.7.3")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += "-Xjsr305=strict"
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

推荐答案

如果您有正确的导入(看起来确实如此),这可能是由缓存问题引起的.

如果您使用的是IntelliJ,则可能需要"使缓存无效并重新启动".你可以在File菜单中找到.这是一张link to the official page美元的支票.

另一件要try 的事情是从/.m2/repository文件夹中删除您的maven缓存.如果文件夹不在默认位置,可以使用以下命令找到它:

mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout

如果使用gradle,您还可以从/.gradle/caches/文件夹中删除该缓存.

Kotlin相关问答推荐

这些Kotlin函数等效吗?

为什么在Spring中,对事务性方法调用的非事务方法调用仍然在事务中运行?

解决Microronaut中多个可能的Bean候选者冲突

升级使用jOOQ Gradle插件生成代码失败

Kotlin Poet 的导入不明确

创建包含 3 个相同项目的列表/使用返回类型重复

在 map 中查找键与在 kotlin 中查找 firstOrNull

为什么会出现Kotlin.Unit错误以及如何修复它?

mutableStateOf 和 mutableStateListOf 有什么区别?

即使 Kotlin 的 `Map` 不是 `Iterable`,它如何以及为什么是可迭代的?

Kotlin:查找集合中最常见的元素

如何在 android jetpack compose 中相互重叠列表项?

如何在 Kotlin 中判断数组类型(不是泛型类型)

Kapt不适用于Android Studio 3.0中的AutoValue

如何在Kotlin中获得KType?

Kotlin out-projected 类型禁止使用

如何根据ArrayList的对象属性值从中获取最小/最大值?

项目不会使用 Kotlin 1.1.3 构建

为什么 Kotlin 会收到这样的 UndeclaredThrowableException 而不是 ParseException?

Kotlin - 如何获取注释属性值