[Java] Scala starting from scratch (environment building mac)

3 minute read

Getting started with Scala

What is Scala?

One of the programming languages.

It has the characteristics of both “object-oriented” and “functional programming” (both of which can do both).

“Goodness” of Scala

  • Can express both object-oriented and functional programming.
  • Design that considers “immutability” of objects
  • High modularity
  • Compatible with Java (Java library can be used)
  • Built-in Future library that can perform asynchronous calculation
  • There is a library Akka for parallel/distributed programming.

Install on Mac

I think it’s easy to use Homebrew.

All the environment construction can be managed (I feel) with this guy.

look for

# scala (main body, but as a prerequisite Java environment is required-see below)
$ brew search scala
==> Formulae
scala [email protected] [email protected] scalaenv scalapack scalariform scalastyle
==> Casks

# sbt (scala build tool, think of it as a useful tool for a lot of things)
$ brew search sbt
==> Formulae
sbt [email protected] sbtenv

Install Java (Assumption)

After all, use Homebrew.

It seems good to install AdoptedOpenJDK.

Adopted OpenJDK is (I think) an OpenJDK binary provided by the Java love community (). Installation via Oracle has become a hassle, like registering an account or arranging folders yourself, so I think it's cheaper to put it in quickly rather than sticking to it strangely.
# Add repository
$ brew tap AdoptOpenJDK/openjdk

# search
$ brew search openjdk

# install
$ brew cask install adoptopenjdk11


Check the location of the installed directory with the java_home command

$ /usr/libexec/java_home -v 11

Add the string that appears as $ nantar /java_home -v 11 to PATH as environment variable JAVA_HOME

If necessary, describe it in .bash_profile or .bashrc.

I wrote it in #.bashrc.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

Reload configuration file

# Below, either one is OK
source ~/.bashrc
exec $SHELL -l

Check it out.

$ java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.7+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode)

$ javac -version
javac 11.0.7

# Compile example
$ javac source.java

# Execution example (Be careful not to include .class)
$ java Source

insert scala

You can use only sbt (described later), but write it for now.

# install
$ brew install scala

that’s all.

Try it

# REPL start
$ scala

Welcome to Scala 2.13.3 (OpenJDK 64-Bit Server VM, Java 11.0.7).
Type in expressions for evaluation. Or try :help.
// output
scala> println("Hello scalalala")
Hello scalalala

// operation 1
scala> 1 + 2
val res1: Int = 3

// operation 2
scala> 3 * 6
val res2: Int = 18

// operation 3
scala> 8.0 / 2.0
val res3: Double = 4.0

// end
scala> :quit
// scala> :q is OK
source file

The extension is .scala


object Main {
  def main(args: Array[String]): Unit = {
println("Hello scala program")
  • Compile and run
$scalac Main.scala
$ scala Main
  • Run without compiling
$ scala Main.scala
  • Run from REPL
$ scala

Welcome to Scala 2.13.3 (OpenJDK 64-Bit Server VM, Java 11.0.7).
Type in expressions for evaluation. Or try :help.
scala> :load Main.scala

insert sbt

# install
$ brew install sbt

that’s all.

Try it

# REPL start
$ sbt console
[info] Starting scala interpreter...
Welcome to Scala 2.12.10 (OpenJDK 64-Bit Server VM, Java 11.0.7).
Type in expressions for evaluation. Or try :help.


(Since the rest is the same, details are omitted)

Compile and run with sbt

I think it’s good to create some folders as a preparation. (The name is myfolder, but anything is fine, please replace the corresponding part)

$ mkdir myfolder
$ cd myfolder

Write and put the source file under myfolder.


object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, Scala World!")

Put the sbt configuration file under myfolder


scalaVersion := "2.12.10"

scalacOptions ++= Seq("-deprecation", "-feature", "-unchecked", "-Xlint")

start sbt

[info] ...

Run with the run command

sbt:myfolder> run
[info] Compiling 1 Scala source to ...
[info] Running HelloWorld
Hello, Scala World!
[success] Total time: 1 s, completed 2015/02/09 15:44:44`

The run command looks for an object that has a main method and executes it.

, or something is wrong with the date...??


Why do you want to ask for a monkey...