Scala Programming – Hello World

This is a continuation from the previous entry which I wrote about the Scala Programming’s setup and IDE installation. In the previous entry, we were successfully setup the JDK, sbt, Scala and installed the IntelliJ. The last step is start coding.

Besides using the IntelliJ, you can install and use other IDEs to write codes, for example, Eclipse. It is just a personal preferences. I asked my friends which tools that they are using for Scala programming and I received a standard reply from them, Eclipse.

As for my Coursera’s course, the instructor is using IntelliJ and I am going to use it in my virtual machine. I will try to use Eclipse as well on my Windows environment. Come, let try to write a Hello World program using the IntelliJ.

When the new project loads, the Project explorer on the left side shows the structure of the Scala project. Go to the src folder and expand it. Under the main folder > New Scala Worksheet > Enter file name. The text editor at the center of the application loads the new file. Type as below,

"Hello World"

And, press the ‘Run’ button on the top. It renders the result on the right side of the application. We need to include the double quotation in our codes.

This is our first helloworld. Different IDEs will show the result in different mode or presentation but the end result is the same. You can try to use the command line to run and print helloworld too. When I tried it using my virtual machine’s command line by typing scala, it prompted me an error. It is my very first time using command line in Ubuntu to run a scala program.

scala

It advised me to install the Scala using the command below. I just did as per advised.

sudo apt install scala

It took a while to install when I run it in my Terminal. Can someone highlight to me what causes the error? Does it mean that Scala installation on the IDEs is not the same as the Terminal?

When the installation is completed in the Terminal, I try again as below and it is correctly displaying the string input I entered.

This is Scala Programming! I am going to stop writing for a while and concentrating on learning this programming language. See ya 🙂

Advertisements

Scala Programming – Setup

As a part of the new environment, I start to learn this functional programming language called Scala Programming. I sign up the Coursera’s course, Functional Programming Principles in Scala. The course began in May 2017.

Therefore, most of my write-up on Scala is be based on the Coursera’s course and some of the experience I gained from my hand-on or on-job-training.

Before I start coding, there is a few things need to be installed and configured before I can start the basic print out “Hello World”. Let me walk through the setup processes here. My machine is running on the virtual machine with Ubuntu 16.04 installed.

Installing the JDK

The first thing first is installing the Java Development Kit. At the point of my learning, its latest version is JDK 8.0. To install JDK on the Ubuntu, we need to run a command in the Terminal. So, let’s open the Terminal and put the below code,

sudo apt-get install openjdk-8-jdk

It will prompt you to enter user password of your machine and it will proceed to get it installed. For other Linux operating systems, there are other commands to be run. To check the installed Java version, you can type the below command in the terminal.

java -version

You can expect to see the detail as below,
ubuntu16-04@ubuntu:~$ java -version
openjdk version “1.8.0_121”
OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)

Alternatively, you can install the JDK manually by downloading the .tar.gz archived file from the Oracle website, unzipped it at the preferred directory and add the bin/ directory of the extracted JDK to the PATH environment variable.

Installing sbt

Okay, what is sbt?

sbt is an open source build tool for Scala and Java projects, similar to Java’s Maven or Ant. This is a definition I found from the Internet. There is an online document you can read up for ‘Getting Started with sbt’, http://www.scala-sbt.org/0.13/docs/Getting-Started.html. I refer to this online document to install sbt on Linux. For other operating systems, you can refer to the relevant document.

echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt

Installing IntelliJ

For the Coursera’s course, the instructor teaches us to install the IntelliJ IDEA. You can choose to install other IDEs such as Eclipse, Sublime and etc. It is your own preferences. For my virtual machine, I will follow the Coursera’s course to use the IntelliJ. The installer can be found from its website, https://www.jetbrains.com/idea/download/#section=linux.

Proceed to download the Community version .tar.gz file. Upon completion, change directory to the file location. You can run the command below to check the file too.

Next, we need to extract the file and begin to install it on our machine by navigating to the bin folder and run the idea.sh. Pardon me for the wrong change directory in the screenshot below.

Viola! The IntelliJ’s first screen prompts up. Go along with the step by step setup until you click the ‘Finish’ button. The customization of IntelliJ IDEA allows us to set the UI Theme, create desktop entry for integration with system application menu, create launcher script which allows us to open files or projects from the command lines, install plugin in which Scala must be installed before we start using it.

So, what is next? Of course we shall create a Scala project and have our very first program runs. Let us give a try and not just finish here… You will get to see UI of IntelliJ with options to a create project, import project and etc on the screen as below,

And yes, we are just two more steps nearer to the first Scala program. Click on the ‘Create New Project’. In the screen below, select the Scala > SBT > Next button.

It brings us to the next page where we fill in the project name and check on the JDK, SBT and Scala properties. Remember that we have done the JDK installation in the first step, then followed by the SBT installation before we began our IntelliJ installation whereby we did the Scala installation too?

Click on the ‘Finish’ button and it creates the project for us. The interface looks as below when it launches on my virtual machine which is running on Ubuntu 16.04.

Last step is start learning how to code! Keep it up!