In order to develop in the functional programming language Scala, you need to set up the toolchain, including the JRE, the scala build tool and an IDE.
In this short tutorial, I will show how to reach this goal.
Install JDK
As a first step, the Java Development Toolkit (JDK) is required on the machine, as Scala is compiled as Java Bytecode. For this, simply download the latest JavaSE distribution for your operating system from the Java download page and install it on your machine (make sure, it really is the „JDK“ distribution, not only the „JRE“ which does not contain the compiler, but only the runtime).
After the installation, also make sure the environment variable „JAVA_HOME“ is set and point to the installation directory of the JDK.
In linux, you can set the environment variable e.g. in the file „.bash_profile“ in your home directory by adding:
1 |
JAVA_HOME="/usr/lib/jvm/java-8-oracle" |
Under Windows, open your „system settings“, then the „extended system properties“, and then the „environment variables“. Enter a new environment variable named „JAVA_HOME“ with the value of the installed JDK, e.g. „C:\Program Files\Java\jdk1.8.0_45“.
Install Typesafe „activator“
For compiling and building Scala applications, there has been developed a toolkit named „sbt“ (short for „Scala Build Tool“), which can also be used for dependency management and deployment. There is an extension to sbt by Typesafe (the company which maintains Scala) called „activator“, which also has a system to create new scala projects from templates and a Web interface for managing and builing applications. I recommend using activator for Scala building.
To get started, just download activator from the Typesafe website. You can choose between the full package which comes with a lot of dependencies, or the minimal package which downloads all missing packages on demand from the web.
To install it, simply extract the downloaded ZIP file to a target directory. Then, if you want to use the activator comfortable form a terminal window, you should add the directory to your „PATH“ environment variable.
Install IDE
There are some IDEs which have suitable language support for Scala. One of them is „Scala IDE“ which is build on top of the well-known and widely used Eclipse IDE. You can download the IDE at scala-ide.org and install it by extracting it to a target directory.
When you start up Scala IDE by „eclipse“ in Linux or „eclipse.exe“ on Windows, you can choose a workspace directory where all your projects will be stored.
Create project
Now, you have set up all your required tools and can start creating your first Scala project.
Basically you now have the option to either use the Activator UI and configure your project with the Activator web interface, or use the commandline to create the project.
To use the web UI, simply start „activator ui“ in a terminal/shell window, given that you have added the activator directory to your „PATH“ environment variable. Under windows, you can also doubleclick on „activator.bat“ in the explorer. Your browser will start up on „localhost:8888“ where the UI is running and you can follow the instructions in the web UI to create your project.
To use the commandline tool, you need to first list all available project templates, by calling „activator list-templates“ in your shell. A longish list of all project templates is displayed. Choose one, e.g. „hello-scala-eclipse“. Then run the command „activator new my-project hello-scala-eclipse“, where „my-project“ is the name of your new project, and „hello-scala-eclipse“ is the chosen project template.
The new project is created as a sub-directory in the current directory of your shell. To enter it, type „cd my-project“ in to the console window.
Now, if you are inside the project directory, you can start „activator“ and it will give you an interactive shell for your project. The first time you start it, it might take a while to download and initialize the required program libraries.
From now on , you can use this commandline, to build, test and package your scala applications. By typing „compile“ in the activator shell, you will try to compile all your scala classes and by entering „run“ you can execute the application, if you have a main class.
Import projects into IDE
Once your activator/sbt project was created, you most likely want to open and edit it in your Scala IDE. The project template „hello-scala-sclipse“ has the required sbt-plugin „sbt-eclipse“ already in place, so you can directly use the „import project“ feature of the Scala IDE to import the existing Scala project into your workspace.
In other project templates you might need to add the sbt-eclipse plugin first. For this, edit or create the file „project/plugins.sbt“ inside your project folder, and enter a line like this:
1 |
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0") |
When you have done this, you need to run „reload“ in your running activator shell once (only if it is still open), and then you can create the required Eclipse/Scala_IDE meta data files by entering „eclipse“ in the activator-shell.