2017-10-27 1 views
1

어떻게 방법 (대화 형) 다음과 같이 실행할 수 있도록 내가 main 정적 선언 할 :코 틀린 확인 방법은 정적 최상위 수준 또는 @JvmStatic 주석입니다

[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlinc 
Welcome to Kotlin version 1.1.51 (JRE 9.0.0.15+181) 
Type :help for help, :quit for quit 
>>> 
>>> println("hello world"); 
WARNING: An illegal reflective access operation has occurred 
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean) 
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory 
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations 
WARNING: All illegal access operations will be denied in a future release 
hello world 
>>> 
>>> :quit 
[email protected]:~/kotlin$ 

컴파일 :

[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlinc Hello.kt 
WARNING: An illegal reflective access operation has occurred 
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean) 
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory 
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations 
WARNING: All illegal access operations will be denied in a future release 
[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlin HelloKt 
error: could not find or load main class HelloKt 
[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ kotlin Hello 
error: 'main' method of class Hello is not static. Please ensure that 'main' is either a top level Kotlin function, a member function annotated with @JvmStatic, or a static Java method 
[email protected]:~/kotlin$ 
[email protected]:~/kotlin$ cat Hello.kt 
class Hello { 


    public fun main(args: Array<String>) { 
     println("Hello, world!" + args[0]) 
    } 
} 
[email protected]:~/kotlin$ 

참조 :

https://kotlinlang.org/docs/tutorials/command-line.html

답변

2

를 튜토리얼, 방법 class Hello이 아니라 최상위 레벨에 선언되었습니다. 또는

import kotlin.jvm.JvmStatic 

object Hello { 
    @JvmStatic 
    public fun main(args: Array<String>) { 
     println("Hello, world!" + args[0]) 
    } 
} 
+0

아아, 그 주석을 추가하려했지만 가져올 수 없습니다. 이 "최고 수준"의 일에는 익숙하지 않습니다 - ㅎ. – Thufir