首页 > 解决方案 > 如何设置 VSCode 以使用 Kotlin 和 Java?

问题描述

我刚刚开始学习如何使用 Kotlin 进行编码,为此,我正在尝试配置 VSCode(版本 1.61),因为它是我的主要 IDE。

我正在使用具有以下特点的电脑:

我已经验证我安装了 java、kotlin 和 kotlic:

$ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2, mixed mode, sharing)
$ kotlin -version
Kotlin version 1.5.31-release-548 (JRE 11.0.11+9-Ubuntu-0ubuntu2)
$ kotlinc -version
info: kotlinc-jvm 1.5.31 (JRE 11.0.11+9-Ubuntu-0ubuntu2)

在 Vscode 中,我安装了以下扩展:

完成所有这些之后,我创建了一个文件夹,其中包含~/Learning/Kotlin/HelloWorld/一个简单的 kotlin 文件"helloworld.kt",其中包含以下代码:

fun fun main(args: Array<String>) {
println("Hello Kotlin")
}

在尝试运行代码时,我收到一个关于找不到 kotlinc 命令的错误,所以我在文件中添加了以下内容~/.bashrc

export KOTLIN_HOME=/home/usr/.sdkman/candidates/kotlin/current
export PATH=$PATH:$KOTLIN_HOME/bin

但是,现在我收到以下错误:

[Running] cd "/home/mantress31/Documents/Manuel/Aprendizaje/Kotlin/Hello World/" && kotlinc helloworld.kt -include-runtime -d helloworld.jar && java -jar helloworld.jar
/home/usr/.sdkman/candidates/kotlin/current/bin/kotlinc: line 98: java: command not found

有人可以告诉我在配置所有这些时我是否做错了什么?和/或我还应该做些什么来解决这个问题?

标签: javalinuxkotlinvisual-studio-codevscode-settings

解决方案


I do not know if this is the best way of solving the problem, but I managed to make it work by doing the following:

First, I installed Oracle's JDK following the instructions in this video: Installing Oracle JDK on Linux.

After that, I went to the .bashrc file (In my pc it is located on the home folder), and added the following: export PATH=$PATH:$JAVA_HOME/bin. After doing this, I reset the file with the source ~/.bashrc.

The error in VSCode was fixed after doing this. Please let me know if there is a better way of doing this.


推荐阅读