新人求助!ide里可以运行,但kotlinc编译报错。


#1

fun prt(name:String){
println("$name" + “加油学习呀!!”)
}
fun main() {
val s1 = “大”
val s2 = “家”
prt(name = s1+s2)
}

代码特简单,初学,见笑了。
我在intellij IDEA里面运行正常,但是编译的时候报错了。
运行环境如下:
kotlin -version
Kotlin version 1.3.72-release-468 (JRE 14.0.2+12-46)
报错信息如下:
kotlinc hello.kt -include-runtime -d hello
Java HotSpot™ 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
hello.kt:5:12: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:19: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:24: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:28: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:35: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:41: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:42: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:44: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:48: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:50: error: expecting member declaration
public static void main(final String[] args) {
^
hello.kt:5:50: error: function declaration must have a name
public static void main(final String[] args) {

实在搞不懂,难道是因为java版本太高?kotlin不能在java 14下运行吗?我需要安装哪个版本的java?


#2

PS D:\java> kotlin
8月 12, 2020 8:35:59 下午 org.jline.utils.Log logr
警告: Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
Welcome to Kotlin version 1.3.72 (JRE 14.0.2+12-46)
Type :help for help, :quit for quit

:load hello.kt
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {
^
error: expecting member declaration
public static void main(final String[] args) {

在REPL环境下也是不能运行,求助各位不吝指点。


#3

自己挖的坑自己填。
kotlin和java一样,程序不是以单个源代码文件存在的。
新建kotlin项目或java项目可以看到会产生一堆目录和一堆文件,包含了源码运行需要的环境。
如果没有这些目录和文件,kotlinc是不能编译源码文件的。
以上。

作为学习kotlin的newbie,我会坚持和加油的,若有大佬看到我问一些幼稚可笑的问题,方便回答的话还望不吝赐教。


#4

不知道


京ICP备16022265号-2 Kotlin China 2017 - 2018