英文:
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 "Hello World!"
}
fun main(args: Array<String>){
println("Hello kotlin")
}
}
application {
// Define the main class for the 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" ))
}
}
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 "Hello World!"
}
companion object {
@JvmStatic
fun main(args: Array<String>){
println("Hello kotlin")
}
}
}
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'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 "Hello World!"
}
}
fun main(args: Array<String>){
println("Hello kotlin")
}
also you need to update your gradle jar task as:
jar {
manifest {
attributes "Implementation-Title": project.name
attributes "Implementation-Version": project.version
attributes "Main-Class": "com.yk.ConsoleGradle.AppKt"
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论