使用Gradle和IntelIJ时,Kotlin中未解析的引用“java”

时间:2018-06-12 06:16:45

标签: java gradle intellij-idea kotlin

我想用kotlin和gradle创建简单的电报机器人。我已经成功导入了ort.telegram库,但它无法解析标准的java库。在gradle.build配置下面:

 NetworkCredential credential = new NetworkCredential("username", "password");
 Service service = new Service("https://exchange-url/EWS/Exchange.asmx", credential);
 Independentsoft.Exchange.Mailbox mailbox = new Independentsoft.Exchange.Mailbox("MysecondEmail@mydomain.com");
 StandardFolderId InboxFolder = new StandardFolderId(StandardFolder.Inbox, mailbox);

Main.kt:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
 */

buildscript {
    ext.kotlin_version = '1.2.41'
    ext.telegramVersion = '3.5'

    repositories {
        maven { url "http://repo.maven.apache.org/maven2/" }
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
       )
    }
}

apply plugin: "java-library"
apply plugin: "kotlin"
apply plugin: "java"


apply plugin: 'application'

mainClassName = 'main.Main'

repositories {
    maven { url "http://jcenter.bintray.com" }
}

dependencies {
    implementation 'org.hibernate:hibernate-core:3.6.7.Final'
    api 'com.google.guava:guava:23.0'
    testImplementation 'junit:junit:4.+'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "org.telegram:telegrambots:3.6"

}


kotlin {
    experimental {
        coroutines "enable"
    }
}

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

gradle安装和构建命令已成功完成。 任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

import声明不完整。您可以导入整个java.util包,就像使用org.telegram.*

一样
import java.util.*

或者更好的是,只导入您需要的类,例如:

import java.util.List

如需完整参考,请参阅Kotlin documentation about packages and imports

作为附注,让IntelliJ通过organizing the imports帮助您。

答案 1 :(得分:0)

我可以假设原因可能如下:你没有在 Main.kt 中指定包

package main // <--- based on your build.gradle file

import org.telegram.*
import org.telegram.telegrambots.TelegramBotsApi
import org.telegram.telegrambots.api.objects.Update
import org.telegram.telegrambots.bots.TelegramLongPollingBot
import java.util.*

fun main(args : Array<String>) {
    println("Hello, world!")
}

以防万一,在 build.gradle 文件中指定 Main.kt 作为主类,您需要将 Kt 后缀添加到类中名:

mainClassName = "main.MainKt"

更新:另一个原因可能是项目结构不正确。在您的情况下,项目结构必须如下:

.
├── build.gradle
└── src
    └── main
        └── kotlin
            └── main
                └── Main.kt