![](/img/trans.png)
[英]Heroku / Kotlin: java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
[英]Gradle Project: java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
我正在處理一個 Java 項目,在這個項目中,我第一次嘗試使用 Kotlin。我開始使用 Intellij Idea 中提供的 JavaToKoltin 轉換器將一些類轉換為 Kotlin。 除其他外,我的自定義異常現在已轉換為 Kotlin。但是有了這個異常處理不再正確工作。
如果我在 java 代碼中拋出我的自定義異常之一(例如MyCustomKotlinException.kt
),則不會捕獲該異常(請參見下面的代碼)。
// Example.java
package foo
import java.util.*;
import java.lang.*;
import java.io.*;
import foo.MyCustomKotlinException;
class Example
{
public static void main (String[] args)
{
try {
// Do some stuff
// if Error
MyCustomKotlinException e = new MyCustomKotlinException("Error Message");
throw e;
} catch (MyCustomKotlinException e) { // <-- THIS PART IS NEVER REACHED
// Handle Exception
} catch (Throwable e) {
e.printStackTrace(); <-- This is catched
} finally {
// Finally ...
}
}
}
所以任何人都可以向我解釋為什么沒有捕獲異常。 MyCustomKotlinException
繼承自 Kotlins RuntimeException
,它只是java.lang.RuntimeException
的別名。
// MyCustomKotlinException.kt
package foo
class MyCustomKotlinException(err: String) : RuntimeException(err)
更新:
我將throw部分拆分成2行(實例創建和throwing),發現問題不在throwing。 try 塊在實例創建后留下。 我創建這個 Kotlin class 的實例有什么問題嗎?
更新2:
我用Throwable
添加了第二個 catch 塊,並捕獲了以下 Throwable。
java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
...
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
更新3:
更改了標題以更正錯誤並修復了將所有項目文件添加到 jar 的問題(請參閱下面的答案)。 將 Kotlin 運行時庫添加到 gradle 對我不起作用。
將所有項目文件添加到 jar 中為我解決了這個問題。 我build.gradle
添加到我的build.gradle
jar {
manifest {
attributes ...
}
// This line of code recursively collects and copies all of a project's files
// and adds them to the JAR itself. One can extend this task, to skip certain
// files or particular types at will
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
更新:更改configurations.compile.collect
到configurations.compileClasspath.collect
根據這個下面的答案。
您需要使用 kotlin 配置您的項目。 所以在 Android Studio 中:
單擊工具 => kotlin => 在項目中配置 kotlin
然后在對話框中檢查:包含 kotlin 文件的所有模塊
並選擇版本
按確定
完成。
這個錯誤很可能是由於簡單的 jar 任務沒有獲取其所有運行時依賴項。
從gradle 文檔中,在您的build.gradle.kts
您可以創建一個“fatJar”任務或將其添加到您的 jar 任務中:
tasks.withType<Jar> {
// Otherwise you'll get a "No main manifest attribute" error
manifest {
attributes["Main-Class"] = "com.example.MainKt"
}
// To add all of the dependencies
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
我要說的是你試圖在沒有kotlin-runtime
庫的情況下運行 Kotlin 代碼
檢查您使用的系統並添加必要的 jar 文件。 您可以通過將您的項目打包到一個.jar
文件並使用運行時庫運行它來驗證這是您的問題
有同樣的問題,在控制台中用 Ant 編譯我的項目。 我已經將 kotlin-stdlib.jar 加入了類路徑,問題就解決了。
添加以下內容為我解決了這個問題:
dependencies {
"kotlinCompilerClasspath"(fileTree("libs/gradle-plugins/kotlin"))
}
這是libs/gradle-plugins/kotlin
:
annotations-13.0.jar
commons-codec-1.9.jar
commons-logging-1.2.jar
gradle-download-task-3.4.3.jar
gson-2.8.5.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
kotlin-android-extensions-1.3.40.jar
kotlin-annotation-processing-gradle-1.3.40.jar
kotlin-build-common-1.3.40.jar
kotlin-compiler-1.3.40.jar
kotlin-compiler-embeddable-1.3.40.jar
kotlin-compiler-runner-1.3.40.jar
kotlin-daemon-client-1.3.40.jar
kotlin-gradle-plugin-1.3.40.jar
kotlin-gradle-plugin-api-1.3.40.jar
kotlin-gradle-plugin-model-1.3.40.jar
kotlin-reflect-1.3.40.jar
kotlin-runtime-1.2.71.jar
kotlin-script-runtime-1.3.40.jar
kotlin-scripting-common-1.3.40.jar
kotlin-scripting-compiler-embeddable-1.3.40.jar
kotlin-scripting-compiler-impl-embeddable-1.3.40.ja
kotlin-scripting-jvm-1.3.40.jar
kotlin-stdlib-1.3.40.jar
kotlin-stdlib-common-1.3.40.jar
kotlin-stdlib-jdk7-1.3.40.jar
kotlin-stdlib-jdk8-1.3.40.jar
kotlinx-coroutines-core-1.1.1.jar
org.jetbrains.kotlin.jvm.gradle.plugin-1.3.40.jar
trove4j-1.0.20181211.jar
完整的gradle.build.kts
(離線設置):
buildscript {
dependencies {
classpath(fileTree("libs/gradle-plugins/kotlin"))
}
}
plugins {
java
`java-library`
}
apply(plugin = "kotlin")
version = "2019.06.1"
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "12"
}
repositories {
flatDir {
dirs("libs/compile")
dirs("libs/provided")
}
}
dependencies {
"kotlinCompilerClasspath"(fileTree("libs/gradle-plugins/kotlin"))
compileOnly(":javaee-api-8.0")
api(":kotlin-stdlib-common-1.3.40")
api(":kotlin-stdlib-1.3.40")
api(":kotlin-stdlib-jdk7-1.3.40")
api(":kotlin-stdlib-jdk8-1.3.40")
api(":gson-2.8.5")
}
感謝您的評論。 確實不推薦使用compile
。 但是,接受的答案不適用於implementation
。 於是查了一下java庫插件的配置和implementation
依賴於compileClasspath。
所以我現在的解決方案是添加
jar {
manifest {
attributes ...
}
// This line of code recursively collects and copies all of a project's files
// and adds them to the JAR itself. One can extend this task, to skip certain
// files or particular types at will
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
和
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50"
//...
}
我覺得這應該由 org.jetbrains.kotlin.jvm 插件來完成。
在 build.gradle 文件的依賴項中使用compile
而不是implementation
為我解決了這個問題。
就我而言,在遷移到 Kotlin 1.3 時, enableFeaturePreview
中的enableFeaturePreview
導致了這個問題。
在我的例子中,添加 maven Bom 文件有效。 它負責下載並保持所有 kotlin 依賴項同步。
imports {
mavenBom 'org.jetbrains.kotlin:kotlin-bom:<<KOTLIN_VERSION>>'
}
參考這里的官方文檔: https://kotlinlang.org/docs/gradle-configure-project.html#other-ways-to-align-versions
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.