CentOS 7
Sponsored Link

Install Scala2017/10/31

 
Install Scala who runs on Java VM and also provides support for functional programming and a static type system.
[1] It's possible to install from CentOS SCLo Software Collections.
# install from SCLo

[root@dlp ~]#
yum --enablerepo=centos-sclo-rh -y install rh-scala210
[2] Packages from Software Collections are installed uder the /opt directory.
To use it, Load environment variables like follows.
# load environment variables

[root@dlp ~]#
scl enable rh-scala210 bash
[root@dlp ~]#
scala -version

Scala code runner version 2.10.6 -- Copyright 2002-2013, LAMP/EPFL
[root@dlp ~]#
which scala

/opt/rh/rh-scala210/root/usr/bin/scala
[3] If you'd like to enable 3.5 automatically at login time, configure like follows.
[root@dlp ~]#
vi /etc/profile.d/rh-scala210.sh
# create new

#!/bin/bash

source /opt/rh/rh-scala210/enable
export X_SCLS="`scl enable rh-scala210 'echo $X_SCLS'`"
[4] Run Scala REPL (Read Eval Print Loop) which is the interactive shell to Test Scala.
[root@dlp ~]#
scala

Welcome to Scala version 2.10.6 (OpenJDK 64-Bit Server VM, Java 1.8.0_151).
Type in expressions to have them evaluated.
Type :help for more information.

# print words
scala> println("Hello Scala World")
Hello Scala World

# set constants
scala> val msg:String = "Hello Scala World"
msg: String = Hello Scala World

scala> println(msg)
Hello Scala World

# exit
scala> sys.exit
[5] Create a sample program to Test Scala.
[root@dlp ~]#
vi readfile.scala
import java.io.File
import java.io.FileReader
import java.io.BufferedReader

object readfile {
  def main(args: Array[String]) {
    val reader = new BufferedReader(new FileReader(new File("/etc/passwd")))
    try {
      var line : String = null
        while ({ line = reader.readLine; line != null }) {
          println(line)
        }
    } finally {
      reader.close
    }
  }
}

# run as a script

[root@dlp ~]#
scala readfile.scala

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....
.....

# compile and run

[root@dlp ~]#
scalac readfile.scala

[root@dlp ~]#
total 16
-rw-------. 1 root root 1441 Dec 19  2016 anaconda-ks.cfg
-rw-r--r--. 1 root root  574 Nov  1 19:44 readfile.class
-rw-r--r--. 1 root root 1104 Nov  1 19:44 readfile$.class
-rw-r--r--. 1 root root  387 Nov  1 19:42 readfile.scala

[root@dlp ~]#
scala readfile

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....
.....
Matched Content