Scala from Scratch 1: Getting Started
In this, the first installment of Scala from Scratch, we will look at how to download, install and set up Eclipse with Scala IDE. We will then create our first "Hello World!" project, run it, and revel in the glory of our powerful new development environment. If you are too impatient to read through this entire article to get to the code, feel free to check out the example project on GitHub.
Downloading & Installing Eclipse
The open-source Scala IDE project provides the best all-around environment for beginners coming to Scala. Unfortunately, the IDE is not distributed as a stand-alone application, we must first install the Eclipse IDE. If you are familiar with Eclipse you can skip ahead to the next section, but be warned that it is a good idea to install the Scala IDE in a separate copy of Eclipse from the one you normally use for development.
For those of us new to Eclipse it can be a bit confusing to find the right package to download, so we'll walk through that first. At the time this article was written, the version we want is Eclipse 3.7, also known by its code name "Indigo." Head over to the site, click the "Downloads" link and you will be presented with a long list of different packages. The one we want is the "Eclipse IDE for Java Developers." Make sure and select the version that matches the operating system and architecture of the machine you are using (32-bit or 64-bit), things can go horribly wrong if you pick the wrong one.
Once the download completes you will have an archive file on your machine, either a zip file or a gzipped tar file depending on your platform. Extract the archive and you'll have a folder named "eclipse" that contains the executable and other elements of the IDE. You should note that there is no installer, the IDE is distributed in that folder, ready-to-run. Move the entire "eclipse" folder to a permanent home on your hard drive, just be sure and keep it somewhere that is writable by your user account. That means on Windows, don't put it in "Program Files", on Linux don't put it in "/usr/local", and OS X users should not put it in "/Applications". We will want to install plug-ins and download updates and that can get hairy if the IDE is placed in the wrong location. The best bet is to keep it somewhere inside your user account's home directory.
Now it's time to start up the IDE and make sure it's all up to date. Windows users should double-click the "eclipse.exe" file in the eclipse folder, OS X users should double-click the "Eclipse" app file. Linux users should run the "eclipse" executable in their preferred manner, either through your window manager or from the command line. When you first launch the IDE it will ask you to select a workspace. This is the location where all of the code we create will be stored, so choose carefully. I recommend selecting the "Use this as the default and do not ask again" check box to prevent this dialog from coming up every time your start the IDE.
After selecting a workspace you'll see a welcome screen, click the arrow icon to go to the workbench and you will finally have made it to the IDE proper. The first thing we need to do is check for any available updates. From the menu bar select "Help" > "Check for Updates". If there are updates available go ahead and walk through the wizard and get them all installed. It will ask you if you want to restart the IDE: always restart when you update Eclipse, your life will be much less stressful.
At this point you should have a fully-functional instance of the Eclipse IDE running on your machine, congratulations! Of course, all we can do with it for the time being is write Java code, not exactly what you were looking for from a series called "Scala from Scratch," eh? So let's forge ahead and set up Scala IDE, then things will finally start to get interesting.
Installing and Using Scala IDE
Back in the early days of Scala, things were a bit complicated. You had to download the compiler and runtime, delve into the command-line, configure the class path for the JVM, sacrifice a goat (or chicken), and invoke the names of long-forgotten deities to bootstrap a project. Fortunately the situation has improved thanks to the good folks over at the Scala IDE project. Now almost all we need to do is add a few plug-ins to our Eclipse install and we're off to the races.
Unfortunately, there is a little bit of configuration file hacking that needs to be done to pave the way for Scala IDE. Eclipse ships with a limit on the amount of memory that it will allocate while it is running, and the default limit is set too low to effectively run the IDE and the Scala compiler. We need to bump the memory limit to at least 1 GB, preferably more if your machine can handle it. I usually configure Eclipse to use about a quater of my total system memory, which for my current workstation works out to be 2 of my available 8 GB. So close your IDE if it's currently running and we will walk through where this setting is and how to change it.
The file we are looking for is called "eclipse.ini" and, on Windows and Linux, is located in the same folder as the eclipse executable we've been launching. On OS X we will have to dig a bit to find it, start by right-clicking on the "Eclipse" app and choose "Show Package Contents." Dig down through "Contents" and into "MacOS" where you will find the elusive "eclipse.ini" file.
Using your favorite text editor open the "eclipse.ini" file and look for the line that starts with "-Xmx=". On Eclipse 3.7 the value will be set to 348m, meaning that the IDE will allocate no more than 348 MB of memory while running. Go ahead and change the number to at least 1024, making sure you do not delete the "m" on the end of the line. Save the file, close your editor and breathe a sigh of relief that we have escaped the realm of the configuration file unscathed.
At long last it is time to install Scala IDE. Head over to the site, click the big "Get it Now" button and scroll down the page until you find the URL for the update site. Copy this URL and head back to your IDE. Once you're back in Eclipse, choose "Help" > "Install New Software..." from the menu bar. Paste the update site URL into the text field of the wizard that pops up and hit enter. A list of features will appear in the wizard, the only one we need right now is "Scala IDE for Eclipse", so select that check box and work your way through the wizard. The plug-ins will install and your will be asked if you want to restart, choose "Restart Now".
Once Eclipse comes back up you will get a dialog asking you if you want to run the Scala setup diagnostics. Choose yes and you will get a small diagnositcs dialog. It is probably a good idea to choose the "Use recommended default settings" option and let Scala IDE configure things like it wants to, at least that's what I do. When you're done click OK. The final step is to click the little square icon in the upper-right of the IDE window (it's tool tip is labeled "Open Perspective"). Choose "Scala" from the menu that appears (you may have to select "Other..." and choose "Scala" from the dialog that pops up). Now you can celebrate, for we have created a working Scala development environment!
Here we see what a fresh install of Scala IDE looks like. On the left is the Package Explorer, that is where your projects, folders, source files and resources will go. On the right is the Outline View, it acts like a table of contents for whatever file you are currently editing, allowing you to easily jump to specific classes, fields and methods. On the bottom are the Problems, Tasks and Console views. These will show compiler errors, tasks you still have to complete and output from running your compiled projects, respectively. The gray area in the center is the editor area and is where the source files you are working on will appear.
Now that the boring bootstrap work is finally done, it's time to actually dive into creating a project, writing code and making our first "Hello World!" program become a reality.
Our First Scala Program: Hello World!
Okay, project creation time is upon us: from the menu bar select "File" > "New" > "Scala Project". The New Project Wizard will pop up: in the "Project Name" field enter "Exercise01" then click "Finish" and your project will be created in the Package Explorer. Drill down into your project and you will see a folder named "src", this is where we will be putting our Scala code. Right-click on that folder and choose "New" > "Package" and enter "hello" in the "Name" field in the dialog that pops up. Click finish and you will see a new package named "hello" appear beneath your "src" folder.
I will note that in this series we will always be creating packages for our Scala code, even when were working on extremely simple exercises like this one. While packages are not strictly required, they are always a good idea. We will get into what packages are and how we can work with them in a future article, just take my word for it right now and always create packages for your code.
At long last, we are going to create a source code file and write a real program. Right-click on your "hello" package and choose "New" > "Scala Object". Enter "HelloWorld" in the "Name" field and click "Finish". You will see a file with the name you specified appear inside the "hello" package and an editor will open in the center of your IDE. The code should look like this:
package hello object HelloWorld { }
The object declaration may look a little strange to developers that are used to declaring classes. This is another topic we will cover at a later date, but for those that obsess over these things know that this is Scala's syntactic sugar for declaring a singleton.
So we have Scala code, but it is not yet a program we can actually run; first we need to tell the runtime that our HelloWorld object is an entry point for the process. In most languages we would declare a function named main(), but Scala has a neat way to this without any magic names. We do this by extending scala.App; this turns the object itself into a kind of script. Go ahead and edit your HelloWorld file to look like this:
package hello object HelloWorld extends App { }
Now we have a valid program, but it doesn't do anything yet. We need to add one more line to complete our epic implementation of "Hello World!" This next line will probably be the most obvious thing you encounter in the rest of your experience with Scala:
package hello object HelloWorld extends App { println("Hello World!") }
I won't bother to explain what this line does, I would hope that it is self-evident.
Time to see this baby in action. Right-click on your "HelloWorld.scala" file in the Package explorer and choose "Run As" > "Scala Application". You should see the Console view at the bottom come to the front and print "Hello World!" Congratulations, you are now a Scala developer! While this exercise has been very simple, we have laid the foundation for all the exploration and discoveries that we will be making in the future.
In the next article we will be diving into expressions and control flow and creating a bit more interactive example project. Follow this blog on Tumblr, subscribe to the RSS feed or follow the exercise repository on GitHub to keep up to date. Good luck and happy hacking!








