gpt4 book ai didi

compilation - 错误:找不到或加载主类Hello.class

转载 作者:行者123 更新时间:2023-12-02 13:09:30 26 4
gpt4 key购买 nike

尝试最简单的Kotlin Hello World:

thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ ll
total 32
drwxr-xr-x 2 thufir thufir 4096 Oct 27 07:28 ./
drwx------ 46 thufir thufir 16384 Oct 27 06:47 ../
-rw-r--r-- 1 thufir thufir 104 Oct 27 07:27 Hello.kt
thufir@dur:~/kotlin$
thufir@dur:~/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
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ kotlin Hello.class
error: could not find or load main class Hello.class
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ cat Hello.kt
class Hello {



fun main(args: Array<String>) {
println("Hello, world!" + args[0])
}
}
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ kotlinc -version
info: kotlinc-jvm 1.1.51 (JRE 9.0.0.15+181)
thufir@dur:~/kotlin$

如何从CLI运行该程序?

所需的输出:
thufir@dur:~/kotlin$ 
thufir@dur:~/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
thufir@dur:~/kotlin$

最佳答案

首先,因为它的Hello函数不是静态的,所以main可能无法按预期工作。在Kotlin中,不需要类来定义main方法。只需使用功能:

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

然后,在编译之后,您不应该像 kotlin <File>.class那样调用它,而只是 kotlin <File>.class后缀是多余的:
$ ./compiler/kotlinc/bin/kotlin HelloKt test
Hello, world!test

关于compilation - 错误:找不到或加载主类Hello.class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com