[JAVA] Scala starting from scratch (environment construction mac)

Get started with Scala

What is Scala?

One of the programming languages.

It has both "object-oriented" and "functional programming" properties (dual wield that can do both).

The "goodness" of Scala

Installation on Mac

I think it's easy to use Homebrew.

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

look for

# scala (Body,However, as a prerequisite, you need an environment where Java can run.-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 guy to do various things)
$ brew search sbt
==> Formulae
sbt                           [email protected]                      sbtenv

Install Java (as a prerequisite)

Again use Homebrew.

ʻIt seems good to install AdoptedOpenJDK`.

AdoptedOpenJDK is an OpenJDK binary (I think) provided by the Java-loving community (). Installing via Oracle has become troublesome, such as registering an account or arranging folders by yourself, so I think it's cheaper to put it in quickly than sticking to strange things ... </ font>

#Add repository
$ brew tap AdoptOpenJDK/openjdk

# search
$ brew search openjdk

# install
$ brew cask install adoptopenjdk11

Let's set the PATH of JAVA_HOME

Check the location of the installed directory with the java_home command

$ /usr/libexec/java_home -v 11

Add the character string that comes out by $ what / java_home -v 11 to PATH as the environment variable JAVA_HOME

Describe it in .bash_profile or .bashrc as needed.

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

Reload configuration file

#Either of the following is OK
source ~/.bashrc
exec $SHELL -l

Check it for the time being.

$ 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

#Compilation example
$ javac source.java

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

Insert scala

You can just use sbt (described later), but I'll write it down.

# install
$ brew install scala

that's all.

Try using

#REPL startup
$ 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> 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

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 using

#REPL startup
$ 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.


(The rest is the same, so details are omitted)

Compile and run with sbt

As a preliminary preparation, I think it's a good idea to create some folders. (I named it myfolder, but anything is fine, please replace the corresponding part)

$ mkdir myfolder
$ cd myfolder

Write the source file under myfolder and put it.


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

Place the sbt config 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 seems to find and execute an object that has a main method.

or something wrong with the date ... ?? </ font>


Recommended Posts