在Gradle项目中混合Java和Kotlin,Kotlin无法找到Java类 [英] Mixing Java And Kotlin In Gradle Project, Kotlin Cannot Find Java Class

查看:908
本文介绍了在Gradle项目中混合Java和Kotlin,Kotlin无法找到Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我正在尝试将Java和Kotlin混合在一个项目中.这里有一个很好的例子. mixed-java-kotlin-hello-world .除了kotlin找不到src/main/java/somepackage/SomeClass.java

As the title says, I am trying to mix Java and Kotlin in a single project. There's a good example found here. mixed-java-kotlin-hello-world. Everything is working properly besides kotlin cannot find any of my Java classes which are found in src/main/java/somepackage/SomeClass.java

这是什么原因?

这是我遇到的错误.

This is the error I am getting.

当我尝试编辑build.gradle时,它表明我没有正确拼写kotlin并在插件上给我错误,但我是从kotlins网站复制和粘贴.

When I try to edit my build.gradle, it shows I'm not spelling kotlin correctly and giving me errors on plugin but I'm copying and pasting from kotlins website.

我的build.gradle看起来像这样

My build.gradle looks like this

group 'Battle-OS'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.2-1"
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'kotlin'

repositories {
    mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.2-1"
testCompile  'junit:junit:4.11'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:1.0.2-1"

compile 'org.slf4j:slf4j-api:1.7.18'
testCompile 'junit:junit:4.12'

    // https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'

// https://mvnrepository.com/artifact/com.google.guava/guava
compile group: 'com.google.guava', name: 'guava', version: '19.0'

// https://mvnrepository.com/artifact/io.netty/netty-all
compile group: 'io.netty', name: 'netty-all', version: '4.0.37.Final'

// https://mvnrepository.com/artifact/com.moandjiezana.toml/toml4j
compile group: 'com.moandjiezana.toml', name: 'toml4j', version: '0.6.0'

// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.0.2-1'

}

与其开始尝试在更大的项目上进行这项工作,不如从头开始.我试图通过创建一个新的gradle项目来完成这项工作.我创建了一个测试Java类,其中包含一种用于打印hello world的方法.然后,我创建了一个测试kotlin类,该类创建了Java类的新对象,并从Java类中调用该方法以打印问候世界.

Instead of starting off trying to make this work on a larger project. I tried to make this work by creating a new gradle project. I created a test java class which contains a method to print hello world. Then I created a test kotlin class which creates a new object of the java class and calls the method from the java class to print hello world.

这解决了我上面遇到的问题,现在我可以从kotlin调用java类,从java调用kotlin,但是现在在运行它时给我错误.

This solves the problem I had above, now I can call java classes from kotlin and kotlin from java but now it gives me errors upon running it.

Exception in thread "main" java.lang.NoClassDefFoundError:      kotlin/jvm/internal/Intrinsics
at AKotlinClassKt.main(AKotlinClass.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

推荐答案

您应像这样覆盖sourceSets

you should override sourceSets like this

sourceSets {
    main.java.srcDirs = []
    main.kotlin.srcDirs = ['src/main/java', 'src/main/kotlin']
    main.resources.srcDirs = ['src/main/resources']
}

这篇关于在Gradle项目中混合Java和Kotlin,Kotlin无法找到Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆