Creating a Scala App

scala

#1

Scala is an object-functional programming language for general software applications.

First create a “Custom” Cloud9 Workspace within your dashboard like explained here.

To get started with Scala on your Cloud9 Workspace, first visit the scala download page to get the link to the latest binaries. At the time of writing, the latest version is 2.11.5 and the link to download it is:

http://downloads.typesafe.com/scala/2.11.5/scala-2.11.5.tgz

Within your workspace terminal type in:

wget http://downloads.typesafe.com/scala/2.11.5/scala-2.11.5.tgz

then unpack the tar by typing:

tar xvf scala-2.11.5.tgz

This creates a folder called scala-2.11.5 within your workspace. Lets move it outside our workspace and into our home folder first, and then set our PATH and SCALA_HOME environment variables.

To move the folder, type in:

mv scala-2.11.5 ../scala-2.11.5

You’ll see the scala-2.11.5 folder disappear from your file tree. You can view the home folder by clicking on the gear icon on the file tree and enabling ‘Show Home In Favorites’ as in the following screenshot

You can expand the ~ folder to see scala-2.11.5 within. Your file tree should look something like the following screenshot now:


Near the bottom of the Home folder, you can see a file titled .profile. Double click on that to start editing it. Append the following lines to the end of the file:

SCALA_HOME="$HOME/scala-2.11.5"
PATH="$PATH:$SCALA_HOME/bin"

Save and close the .profile file. For the changes to take effect, you have to restart your terminal. To restart your terminal, just right-click on an empty space within the terminal pane and select ‘Restart All Terminal Sessions’ as shown below:


Now typing in scala within your terminal should start Scala’s Interactive Shell.

Creating a simple Scala hello world app


Create a new file titled ‘hello-world.scala’ and paste in the following code:

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

In the terminal, lets make a folder to output class files

mkdir classes

Now, we can compile by typing in:

scalac -d classes hello-world.scala

and run the program (specifying classpath with -cp) by typing in

scala -cp classes HelloWorld

which will output ‘Hello, world!’ as expected :+1:


Tutorials - Table of Contents