简体   繁体   English

尝试在IntelliJ IDEA中运行Kotlin脚本

[英]Trying to run Kotlin script in IntelliJ IDEA

I am learning Kotlin by doing Kotlin Koans int IntelliJ EduKotlin plugin ( https://plugins.jetbrains.com/plugin/8186 ). 我正在通过做Kotlin Koans int IntelliJ EduKotlin插件( https://plugins.jetbrains.com/plugin/8186 )学习Kotlin。 While doing tasks and running them im this plugin is working, I am not able to create new Kotlin script and run it without this plugin. 在执行任务并在此插件运行时运行它们的同时,我无法创建新的Kotlin脚本并在没有此插件的情况下运行它。

Sample code in a file named Hello.kt : 名为Hello.kt的文件中的示例代码:

fun hello() : String {
    return "ok"
}

Then I create new run configuration derived from 'Kotlin script', set 'Working directory' to ProjectName/src folder (or ProjectName folder, same result) and IDE shows me a 'Could not find script file: Hello.kt' warning as in the screen below: 然后我创建派生自“ Kotlin脚本”的新运行配置,将“工作目录”设置为ProjectName / src文件夹(或ProjectName文件夹,结果相同),并且IDE向我显示“找不到脚本文件:Hello.kt”警告,如下所示下面的屏幕:

在此处输入图片说明

And indeed, while trying to run script I got following stacktrace: 确实,在尝试运行脚本时,我得到了以下堆栈跟踪:

"C:\Program Files\Java\jdk1.8.0_74\bin\java" -Dfile.encoding=windows-1250 -classpath C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-runtime.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -script Hello.kt
exception: java.lang.RuntimeException: Failed to evaluate script: kotlin.KotlinNullPointerException
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:263)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileAndExecuteScript(KotlinToJVMBytecodeCompiler.kt:212)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:181)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:49)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:181)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:138)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:57)
at org.jetbrains.kotlin.cli.common.CLICompiler.doMainNoExit(CLICompiler.java:248)
at org.jetbrains.kotlin.cli.common.CLICompiler.doMain(CLICompiler.java:238)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(K2JVMCompiler.kt:248)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(K2JVMCompiler.kt)
Caused by: kotlin.KotlinNullPointerException
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:475)
... 10 more


Process finished with exit code 2

What more, the hello method is marked as never used. 而且,hello方法被标记为从不使用。 However, when I change file extension from .kt to .ktscript, then this method is marked as used, and the output from running code is following: 但是,当我将文件扩展名从.kt更改为.ktscript时,此方法被标记为已使用,并且运行代码的输出如下:

"C:\Program Files\Java\jdk1.8.0_74\bin\java" -Dfile.encoding=windows-1250 -classpath C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-runtime.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -script Hello.kt

error: source file or directory not found: Hello.kt 错误:找不到源文件或目录:Hello.kt

Process finished with exit code 1

So my question is: what am I missing when trying to create and run Kotlin code in a standalone Kotlin script in IntelliJ? 所以我的问题是:当尝试在IntelliJ中的独立Kotlin脚本中创建和运行Kotlin代码时,我会缺少什么?

A standalone Kotlin script file needs to have a .kts extension. 独立的Kotlin脚本文件需要具有.kts扩展名。 If you rename your file, you also need to make sure that your run configuration is updated to refer to the new name of the file. 如果重命名文件,则还需要确保运行配置已更新为引用文件的新名称。

Also, if you create any file (a regular Kotlin file or a .kts file) which only contains a function, running the script will not produce any output, because the function will not be called. 另外,如果您创建仅包含函数的任何文件(常规Kotlin文件或.kts文件),则运行脚本不会产生任何输出,因为不会调用该函数。 To see some output, you need to call the function in your script: 要查看一些输出,您需要在脚本中调用该函数:

fun hello(): String {
    return "ok"
}

println(hello())

The stacktrace that you saw is a bug in Kotlin . 您看到的stacktrace是Kotlin中的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM