Skip to main content

Command Palette

Search for a command to run...

Scala : First project - Hello World

Updated
2 min read
Scala : First project - Hello World

create first project

Using the command line to create first project
sbt is a build tool for Scala. sbt compiles, runs, and tests your Scala code. (It can also publish libraries and do many other tasks.)

To create a new Scala project with sbt:

  • cd to an empty folder.
  • Run the command sbt new scala/scala3.g8 to create a Scala 3 project, or sbt new scala/hello-world.g8 to create a Scala 2 project.
    This pulls a project template from GitHub. It will also create a target folder, which you can ignore.
  • When prompted, name the application HelloWorld.
    This will create a project called “helloworld”. Seems it change the letter automaticlly.

Let’s take a look at what just got generated:

scala_firstproject_3.png

  • helloworld
    • project (sbt uses this for its own files)
      • build.properties
    • build.sbt (sbt's build definition file)
    • src
      • main
        • scala (all of your Scala code goes here)
          • Main.scala (Entry point of program) <-- this is all we need for now
      • test
        • scala
          • Mysuite.scala

run first project

run the application from a terminal with these steps:

  1. cd into hello-world.
  2. Run sbt. This opens up the sbt console.

scala-helloworld-1.png 3.Type ~run. The ~ is optional and causes sbt to re-run on every file save, allowing for a fast edit/run/debug cycle. sbt will also generate a target directory which you can ignore.

scala-helloworld-2.png

4.You can see the "Hello World" message.When you’re finished experimenting with this project, press [Enter] to interrupt the run command. Then type exit or press [Ctrl+D] to exit sbt and return to your command line prompt.
scala-helloworld-3.png

with an IDE

You can choose multiple IDE to edit Scala program. I use VS Code.
Open VS code, go to Extensions, search and install "Metals".
image.png Metals is a “Scala language server” that provides support for writing Scala code in VS Code and other editors like Atom, Sublime Text, and more, using the Language Server Protocol.
Under the hood, Metals communicates with the build tool by using the Build Server Protocol (BSP). For details on how Metals works, see, “Write Scala in VS Code, Vim, Emacs, Atom and Sublime Text with Metals”.

Then open the project folder. You can see two files.

build.sbt
src/man/scala/Main.scala

image.png Main.scala stores your source code.

image.png build.sbt stroes the configuration for run the source code.

Resources:

https://docs.scala-lang.org/getting-started/index.html#open-hello-world-project

More from this blog

机器学习中的线性代数基础(Linear Algebra for Machine Learning and Data Science)

为什么要学习machine learning所需要的数学基础? 1.了解数学概念,更好的理解机器学习算法 2.深入理解后可以自定义算法而不是应用现成算法,同时能理解其优点、局限性及应该如何运用 3.可以运用数学工具优化算法 第一周:线性方程组 线性方程应用举例:神经网络 句子系统——与方程组合相似 完整系统:系统包含与句子一样多的信息量,无冗余无矛盾。非单一系统/非奇异系统。 冗余系统-包含冗余重复信息。单一/奇异系统 矛盾系统-包含矛盾信息。单一/奇异系统 2变量的线性方程组 对应完整系统——...

May 19, 20231 min read
机器学习中的线性代数基础(Linear Algebra for Machine Learning and Data Science)

Yuan Li's Tech Blog

14 posts

Hi,I am a full stack developer. I am also interested in Programming Language, Machine Learning, Data Visualization, Cyber Security and Algorithm.