Sunday, November 16, 2014
This article provides a quick start with Scala and to enables to write your first program with Scala. 

Scala Installation

Scala Pre-requisites

To install the Scala software distribution we need Java run-time version 1.6 or higher. You can check this using "java -version" from the command line.

C:\Users\mad>java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

Upgrade Java if needed from this link.

Scala Binaries

Download the Scala binaries from this link. For Windows it is a MSI installer package and pretty straightforward to install. For other platforms download and unpack the archive. For quick access add scala (Scala Interpreter) and scalac (Scala Compiler) to the path settings.

For Windows,
set SCALA_HOME=C:\Installs\scala

For Unix,
export SCALA_HOME=/usr/installs/scala

Scala IDE for Eclipse

The easiest way to get started with Scala and be productive is to use Scala IDE for eclipse. Download and install the eclipse bundle from this link for the required Scala version.

Scala First Program

There are several approaches to write you first program. (1) Using the Scala interactive shell (2) Using compile and execute and (3) Using Eclipse IDE.

Using the Scala interactive shell

Simply open the interactive shell by typing the command "scala" from the command line. Enter the commands below interactively to execute your first program.

Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object Hello {
     | def main(args: Array[String]) {
     | println("Hello")
     | }
     | }
defined object Hello

scala> Hello.main(null)

scala> :q

Using compile and execute

Create a file with the following contents and name it "SayHello.scala".

object SayHello {
    def main(args: Array[String]) {

Now compile the program using scalac the Scala compiler and execute using scala command. The scala compiler works similar to the java compiler and generates class files in the current directory.

C:\Users\mad>scalac SayHello.scala

C:\Users\mad>scala SayHello

Using the Scala Eclipse IDE

Using the Scala Eclipse IDE Create a Scala project using "File -- New -- Scala Project" in the IDE.

One Scala project is created select the SayHello project in IDE and Create a new Scala object using "File -- New -- Scala Object".

Enter SayHello program listing below and do RunAs -- Scala Application to generate the output.

Congratulations you are done with your first Scala program. Proceed to read other Scala Tutorials.


Post a Comment

Contact Form


Email *

Message *

Back to Top