[英]android studio “error: can't find or load main class” in Kotlin file's main method
In my Android Studio project's java module,I used to code java's main method is OK,But when i convert it to Kotlin code ,like this : 在我的Android Studio项目的Java模块中,我曾经编码过Java的主要方法是可以的,但是当我将其转换为Kotlin代码时,就像这样:
fun main(args: Array<String>) {
println("i am kotlin")
}
When i executed it, the error happend:"error: can't find or load main class". 当我执行它时,发生了错误:“错误:找不到或加载主类”。 I have tried to find some way to solve it ,for example:let the main method outside the class,But it does not work.
我试图找到某种方法来解决它,例如:让main方法在类外,但是它不起作用。 I want to know whether it is bebcause that my configuration has some problems.
我想知道是否是因为我的配置有问题。 When i use IntelliJ IDEA i build a kotlin project ,the main method is OK.
当我使用IntelliJ IDEA构建一个Kotlin项目时,主要方法是确定。 so what is my problem in my Android studio's java module in kotlin code.
那么我在Android Studio的kotlin代码中的java模块中遇到了什么问题。
In kotlin, the main function should be outside of a class which didn't work for you. 在kotlin中,主要功能应该在对您不起作用的类之外。 So when you're trying to use the main function of a class it has to be wrapped up by companion object also be annotated by
@JvmStatic
. 因此,当您尝试使用类的主函数时,它必须由伴随对象包装起来,并由
@JvmStatic
进行注释。 So the code block will look like this. 因此,代码块将如下所示。
class AnythingYouWant {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println("i am kotlin")
}
}
}
Another thing to remind you that, 另一件事提醒您,
The main method isn't the entry point in Android like in Java
主要方法不是Android中像Java中的切入点
because it uses completely different build configuration in Android Studio project than the IntelliJ project. 因为它在Android Studio项目中使用与IntelliJ项目完全不同的构建配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.