Kotlin应用程序与Gradle应用程序插件 [英] Kotlin Application with Gradle application plugin
问题描述
我试图用 kotlin
, gradle
和gradle <$ c $创建一个简单的HelloWorld应用程序c>应用程序插件。当我用下面的设置运行它时,出现以下错误:
错误:Main方法在类com.petarkolaric中不是静态的.helloworld.Main,请将主要方法定义为:
public static void main(String [] args)
我的 build.gradle
:
group'helloworld '
version'1.0-SNAPSHOT'
buildscript {
ext.kotlin_version ='1.2.0'
知识库{
mavenCentral )
}
依赖项{
classpathorg.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version
}
}
apply plugin:'kotlin'
apply plugin:'application'
mainClassName =com.petarkolaric.helloworld.Main
repositories {
mavenCentral()
}
依赖关系{
compileorg.jetbrains.kotlin:kotlin-stdlib-jdk8:$ kotlin_version
}
compileKotlin {
kotlinOption s.jvmTarget =1.8
}
compileTestKotlin {
kotlinOptions.jvmTarget =1.8
}
我的 src / main / kotlin / com / petarkolaric / helloworld / Main.kt
:
package com.petarkolaric.helloworld
class Main {
fun main(args:Array< String> ;){
println(Hello,World!)
}
}
根据此博文我应该能够以这种方式使用应用程序插件。
我需要更改以允许应用程序
插件来运行我的主要功能,当我运行 gradle run
?
如错误所述,您的主要方法不是静态的。您有以下选择:
1)将主要元素放入伴随对象
中并使其 JvmStatic
class Main {
伴随对象{
@JvmStatic
fun main(args:Array< String>){
println(Hello,World!)
}
}
}
2)将您的类
更改为 object
- 比你多或少有一个单例
class
,并使它成为 JvmStatic
object Main {
@JvmStatic
fun main(args:Array< String>){
println(你好,世界!)
}
}
3)将main在课堂外
fun main(args:Array< String>){
println(Hello,World! )
}
class Main {
}
I am trying to create a simple HelloWorld application using kotlin
, gradle
, and the gradle application
plugin. When I run it with the below setup I get the following error:
Error: Main method is not static in class com.petarkolaric.helloworld.Main, please define the main method as:
public static void main(String[] args)
My build.gradle
:
group 'helloworld'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = "com.petarkolaric.helloworld.Main"
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
My src/main/kotlin/com/petarkolaric/helloworld/Main.kt
:
package com.petarkolaric.helloworld
class Main {
fun main(args : Array<String>) {
println("Hello, World!")
}
}
According to this blog post I should be able to use the application plugin this way.
What do I need to change to allow the application
plugin to run my main function when I run gradle run
?
As the error says, your main method is not static. You have the following options:
1) Put the main into the companion object
and make it JvmStatic
class Main {
companion object {
@JvmStatic
fun main(args : Array<String>) {
println("Hello, World!")
}
}
}
2) Change your class
to object
- than you more or less have a singleton class
and make it JvmStatic
object Main {
@JvmStatic
fun main(args : Array<String>) {
println("Hello, World!")
}
}
3) Place the main outside the class
fun main(args : Array<String>) {
println("Hello, World!")
}
class Main {
}
这篇关于Kotlin应用程序与Gradle应用程序插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!