无法获得Kotlin的"hello world"在intelliJ中运行的示例 [英] Can't get kotlin "hello world" example to Run in intelliJ

查看:75
本文介绍了无法获得Kotlin的"hello world"在intelliJ中运行的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注名为

这是我的代码:

  fun main(args:Array< String>){println(您叫什么名字?")val名称= readLine()println("H​​ello $ name")} 

这是我的项目结构:

我注意到它说未配置Kotlin".上面的代码,但是当我进入 Tools>科特林在Project中配置Kotlin ,我得到

:

在事件日志"窗格中,我看到以下内容:

  4/15/21下午1:54下载预构建的共享索引通过预构建的JDK共享索引减少索引时间和CPU负载一律下载下载一次不再显示配置...1:54 PM Gradle同步开始1:54 PM Gradle同步失败:不支持的班级文件主要版本60(5 s 329 ms)下午1:55没有可用的IDE或插件更新 

在问题(?)"窗格中,我看到了:

 未解决的参考:println未解决的参考:readLine未解决的参考:println从未使用参数"args"包指令与文件位置不匹配 

关于为什么我的程序中的基本功能无法识别的任何想法?

感谢您的光临!

我尝试下载JDK 15并在我的项目结构中指定它,但是得到了相同的结果:

解决方案

好的,我知道了.我会努力弄清楚我做了什么.

tldr;我必须从 JDK 16 切换到 JDK 15 才能运行hello world控制台应用程序.

首先,当我设置项目时,我选择了错误的项目模板:我选择了 JVM-Application ,而不是 JVM-Console Application .这是我需要做的:

因此,我开始了一个新项目,选择了正确的模板,然后输入了代码,我注意到显示在左下方的 Build 窗格:

我尝试了 Build>构建项目,没有任何改变.因此,我尝试了 Run 菜单项:运行"菜单的第一部分显示为灰色,但是在运行"菜单的第二部分中还有另一个运行"命令.我选择了运行",然后弹出一个带有以下内容的小窗口:

 运行0编辑配置 

"0编辑配置"突出显示了一行,而我却无所适从试图弄清​​楚如何运行我的代码.

因此,我开始了另一个新项目,这次我选择了另一个版本15的JDK:

在单击 Next ,然后单击 Finish 之后,我输入了我的代码,这一次的Build窗格如下所示(除了输入我的代码外,我无需做任何其他事情)):

按照教程所描述的那样,我在输入完代码后,这次在沟槽中出现了一个绿色的小箭头:

当我单击装订线中的绿色箭头时,我可以成功运行程序.

因此,看来您不能使用 JDK 16 ,而必须使用 JDK 15 .intelliJ能够检测到我所有已安装的Java版本,因此您已安装的任何版本都应作为项目设置中的一个选项出现.您可能必须退出intelliJ,然后在安装JDK 15之后重新启动intelliJ,以便在创建项目时看到它作为 Project JDK:下的选项列出.

I'm following the intelliJ IDEA tutorial titled Create your first Kotlin application (Last modified: 08 March 2021). Here are the various versions I'm using:

~$ java -version
java version "16" 2021-03-16
Java(TM) SE Runtime Environment (build 16+36-2231)
Java HotSpot(TM) 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)

~$ javac -version
javac 16


Here's my code:

fun main(args: Array<String>) {
    println("What is your name?")
    val name = readLine()
    println("Hello $name")
}

Here's what my project structure looks like:

I notice that it says "Kotlin not configured" above my code, but when I go to Tools > Kotlin > Configure Kotlin in Project, I get

:

In the Event Log pane, I see this:

4/15/21
1:54 PM Download pre-built shared indexes
                Reduce the indexing time and CPU load with pre-built JDK shared indexes
                Always download
                Download once
                Don't show again
                Configure...

1:54 PM Gradle sync started

1:54 PM Gradle sync failed: Unsupported class file major version 60 (5 s 329 ms)

1:55 PM No IDE or plugin updates available

in the Problems(?) pane, I see this:

Unresolved reference: println
Unresolved reference: readLine
Unresolved reference: println
Parameter 'args' is never used
Package directive doesn't match file location

Any ideas on why the basic functions in my program are not recognized?

Thanks for looking!

Edit: I tried downloading JDK 15 and specifying that in my Project Structure, but I got the same results:

解决方案

Okay, I got it working. I'll try to figure out what I did.

tldr; I had to switch from JDK 16 to JDK 15 to get my hello world console application to run.

First, when I was setting up the project, I chose the wrong Project Template: I selected JVM - Application rather than JVM - Console Application. This is what I needed to do:

So, I started a new project, chose the right template, and I typed in my code, and I noticed a Build pane at the bottom left displayed:

I tried Build > Build Project, and nothing changed. So, I tried the Run menu item: the first section of the Run menu was grayed out, but there was another Run command in the second section of the Run menu. I selected that Run, and a little window popped up with the following:

      Run
  0  Edit Configurations

The "0 Edit Configurations" line was highlighted, and I got nowhere trying to figure out how to Run my code.

So, I started yet another new project, and this time I selected a different JDK, version 15:

After clicking Next, then Finish, I typed in my code, and this time the Build pane looked like the following (without me doing anything but typing in my code):

After I finished typing in my code, as the tutorial described, this time there was a little green arrow in the gutter:

When I clicked on the green arrow in the gutter, I could successfully Run my program.

So, it appears that you cannot use JDK 16, and you have to use JDK 15. intelliJ was able to detect all my installed Java versions, so any version you have installed should appear as a choice in the the project settings. You may have to quit intelliJ, then restart intelliJ after you install JDK 15 in order to see it listed as a choice under Project JDK: when you create a project.

这篇关于无法获得Kotlin的"hello world"在intelliJ中运行的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆