( “https://i.stack.imgur.com/EykkL.png”)
apply plugin: 'java'
apply plugin: 'kotlin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
buildscript {
ext.kotlin_version = '1.1.2-4'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
// override the sourceSet flow other answers.
sourceSets {
main.java.srcDirs+= 'src/main/java'
main.kotlin.srcDirs+= 'src/main/kotlin'
main.resources.srcDirs += 'src/main/resources'
}
它可能是成功编译的,但是当我运行kotlin代码时。 它抛出了下面的例外。并且java代码运行正常。
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at KotMainKt.main(kotMain.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
答案 0 :(得分:0)
我在build.gradle中添加以下代码并修复此问题。
链接:Configuring Gradle with Kotlin
jar { 清单{ 属性'Main-Class':'KotMainKt' }
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}