Saturday, October 5, 2013

First Script in Scala

This is a quick summary on how to build and run a very simple application in Scala. The application is a typical "Hello World" example so there no Scala to learn here, instead I hope it will make it frictionless to download, install and configure the essentials for creating the very first Scala application. In this example the OS is Fedora 19 and I will use IntelliJ IDEA as IDE (Integrated Development Environment).

Begin with downloading the JAVA JDK, you'll need this to start the IDE and to make the application. Get the latest RPM-package here (x64 or x86 depending on the OS)


Install from a terminal (replace path to the download and the JDK version)

rpm -Uvh /path/jdk-7u40-linux-x64.rpm

Download IntelliJ IDEA for Linux here:


Extract the file, i.e:  tar -xzf ideaIC-xx.xx.xx.tar.gz

Before the IDE can be started an environment varible to the JDK need to be set. Either one of IDEA_JDK, JDK_HOME or JAVA_HOME will work. This is one way of doing that:


export IDEA_JDK=/usr/java/jdk1.7.0_40 (make sure it's the correct path to the root of the JDK installation)

For the environment variable to persist it needs to added to a config file. There are plenty of tutorials with instructions for this!

Start the IDE from the terminal window:  ./idea.sh

In the start window navigate to Configure -> Plugins, then click Install JetBrains Plugin and search for Scala



Right click and Download and Install!

Go back to the Start page and create a New Project. Choose Scala Module. Enable 'Set Scala Home' and click 'Download Scala'. Choose a version to Download. After the download set the path to the download folder. Make sure Compiler library and Standard library is filled out. In case there is a warning triangle as in the picture below, ignore it for now. Click Finish!


 
Navigate to File -> Project Structure -> Libraries, and fix the errors if any (There is a small icon to click for fixing the error). Dependencies to scala-compiler and scala-library must be added as in the picture below.



Add a new Scala script to the project, name it FirstScript.scala
The script file must be added to the src folder for the project to run!

Type some program code in a main method, something like this:

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

Build! (Alt+F9)
Run! (Shift+F10)

Good Luck!!





No comments:

Post a Comment