Scala on Visual Studio Code

[comment]: # Scala on Visual Studio Code

Download and install Scala

Download a scala installation package from here.
Then install it.

  • Linux
scala_package_name=$(ls scala*.tgz | sort -r | head -1)
tar -xzf $scala_package_name
mv ${scala_package_name%.*} scala

Configure system variables:

  • Linux
export SCALA_HOME=/opt/scala
PATH=%PATH%:$SCALA_HOME/bin
  • Windows
SCALA_HOME=C:\Program Files (x86)\scala
PATH=%SCALA_HOME%\bin;%PATH%

Test

scala
  • Output:
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_60).
Type in expressions for evaluation. Or try :help.

scala>

Configur a project in visual studio code

  • Open a project via File -> Open Folder...
  • Create a tasks.json file under the .vscode folder in the project folder.
  • Input below in the task.json file
// A task runner that runs a scala program
{
    "version": "0.1.0",
    "isShellCommand": true,
    "args": [],
    "showOutput": "always",
    "echoCommand": true,
    "suppressTaskName": true,
    "windows": {
        "command": "cmd",
        "args": [
            "/C",
            "scala.bat"
        ]
    },
    "linux": {
        "command": "sh",
        "args": [
            "scala"
        ]
    },
    "osx": {
        "command": "sh",
        "args": [
            "scala"
        ]
    },
    "tasks": [
        {
            "taskName": "run",
            "isBuildCommand": true,
            "args": [
                "${file}"
            ]
        }
    ]
}

Note: I am using Windows, you need to change scala.bat to scala (I guess).

Linux

Test it

  • Create a file test.scala with code
object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}
  • press ctrl+shift+b
  • Output:
Hello, world!

Compile .scala to .jar

scalac -d test.jar D:\project\*
posted @ 2016-09-08 14:40  SNYang  阅读(7164)  评论(1编辑  收藏  举报