0

I wanted to create simple telegram bot using kotlin and gradle. I have successfully imported ort.telegram library, but it cannot resolve standard java libraies. Below gradle.build configuration:

/*
 * 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"
    }
}

Main.kt:

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!")
}

The gradle install and build commands are successfully completed. Any help will be appreciated

2 Answers2

0

The import statement is incomplete. You could import the whole java.util package, like you do with org.telegram.*:

import java.util.*

Or better still, import only the classes you need, for example:

import java.util.List

For full reference, see the Kotlin documentation about packages and imports.

As a side note, let IntelliJ help you by organizing the imports.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
0

I can assume that the reason may be as follows: you did not specify the package in 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!")
}

Just in case, in build.gradle file to specify Main.kt as main class you need to add Kt suffix to a class name:

mainClassName = "main.MainKt"

UPDATE: another reason may be an incorrect project structure. In your case, project structure has to be as follows:

.
├── build.gradle
└── src
    └── main
        └── kotlin
            └── main
                └── Main.kt
Ivan Samborskii
  • 240
  • 1
  • 4