错误:主类中的 main 方法不是静态的。Kotlin 项目

huangapple go评论78阅读模式
英文:

Error: Main method is not static in class. Kotlin Project

问题

// 以下是翻译好的内容:

我需要从 Kotlin 项目创建一个可运行的 Jar 文件它是一个控制台应用程序我使用 gradle init 设置了这个项目我尝试了所有我能在网上找到的解决方案比如将主方法从类中移出在伴生对象中包装它并使用 JvmStatic 注解但我仍然不断收到一个错误消息"Error: Main method is not static in class com.yk.ConsoleGradle.App, please define the main method as: public static void main(String[] args)"

我正在使用 ./gradlew jar 来构建这个项目

class App {
    val greeting: String
        get() {
            return "Hello World!"
        }
   
    fun main(args: Array<String>){
        println("Hello kotlin")
    }
}

application {
    // 为应用程序定义主类。
    mainClass.set("com.yk.ConsoleGradle.AppKt")
}
tasks.jar{
    manifest {
        attributes(mapOf("Implementation-Title" to project.name,
                "Implementation-Version" to project.version, "Main-Class" to "com.yk.ConsoleGradle.App" ))

    }
}

这是 jar tf 的输出结果
jar tf /ConsoleGradle/app/build/libs/app-1.0.1.jar 
META-INF/
META-INF/MANIFEST.MF
META-INF/app.kotlin_module
com/
com/yk/
com/yk/ConsoleGradle/
com/yk/ConsoleGradle/AppKt.class
com/yk/ConsoleGradle/App.class

非常感谢您的帮助
英文:

I need to create a runnable Jar file which is a console app from the Kotlin project. I set up this project using gradle init. I tried all solutions I could find online by moving the main method out of the class, wrapping it in a companion object, and using JvmStatic annotation but I still keep getting an error message "Error: Main method is not static in class com.yk.ConsoleGradle.App, please define the main method as:
public static void main(String[] args)"

I am using ./gradlew jar to build the project

class App {
    val greeting: String
        get() {
            return &quot;Hello World!&quot;
        }
   
    fun main(args: Array&lt;String&gt;){
        println(&quot;Hello kotlin&quot;)
    }
}

application {
    // Define the main class for the application.
    mainClass.set(&quot;com.yk.ConsoleGradle.AppKt&quot;)
}
tasks.jar{
    manifest {
        attributes(mapOf(&quot;Implementation-Title&quot; to project.name,
                &quot;Implementation-Version&quot; to project.version, &quot;Main-Class&quot; to &quot;com.yk.ConsoleGradle.App&quot; ))

    }
}

Here is the output of jar tf

jar tf /ConsoleGradle/app/build/libs/app-1.0.1.jar 
META-INF/
META-INF/MANIFEST.MF
META-INF/app.kotlin_module
com/
com/yk/
com/yk/ConsoleGradle/
com/yk/ConsoleGradle/AppKt.class
com/yk/ConsoleGradle/App.class

Any help appreciated.

答案1

得分: 2

我不确定这是否是你所做之事的正确方法,但不妨考虑一下:

class App {
    val greeting: String
        get() {
            return "Hello World!"
        }
    companion object {
        @JvmStatic
        fun main(args: Array<String>){
            println("Hello kotlin")
        }
    }
}

companion object 使得函数可以通过类访问,而不是通过实例访问,而且 JvmStatic 注解会为 Java 互操作生成一个静态方法。

英文:

I don't know if this is the right approach for what you're doing, but how about

class App {
    val greeting: String
        get() {
            return &quot;Hello World!&quot;
        }
    companion object {
        @JvmStatic
        fun main(args: Array&lt;String&gt;){
            println(&quot;Hello kotlin&quot;)
        }
    }
}

The companion object akes the function accessible through the class instead of through an instance, and the JvmStatic annotation generates a static method for Java interop

答案2

得分: 2

如果您不想在 App 类内声明 main 函数您可以创建一个名为 App.kt 的文件其中包含以下代码

class App {
    val greeting: String
        get() {
            return "Hello World!"
        }
}

fun main(args: Array<String>){
    println("Hello kotlin")
}

另外,您需要将 gradle 的 jar 任务更新为:

jar {
    manifest {
        attributes "Implementation-Title": project.name
        attributes "Implementation-Version": project.version
        attributes "Main-Class": "com.yk.ConsoleGradle.AppKt"
    }
}

注意,Main-Class 被指定为 AppKt,而不是 App。
这是因为 main 函数是一个顶层函数,对于 App.kt 内部的顶层函数,Kotlin 编译器会创建一个名为 AppKt.class 的类。


<details>
<summary>英文:</summary>

If you don&#39;t want to declare the main function inside the App class you could just create an App.kt file with the following code:
```kotlin
class App {
    val greeting: String
        get() {
            return &quot;Hello World!&quot;
        }
}

fun main(args: Array&lt;String&gt;){
    println(&quot;Hello kotlin&quot;)
}

also you need to update your gradle jar task as:

jar {
    manifest {
        attributes &quot;Implementation-Title&quot;: project.name
        attributes &quot;Implementation-Version&quot;: project.version
        attributes &quot;Main-Class&quot;: &quot;com.yk.ConsoleGradle.AppKt&quot;
    }
}

notice the Main-Class is specified as AppKt instead of App.
This is because the main function is a top-level function and for top-level functions inside App.kt the Kotlin compiler creates a class called AppKt.class.

huangapple
  • 本文由 发表于 2020年10月22日 22:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64484231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定