Scala + Eclipse + sbt on a Mac

Scala is now the official general-purpose programming language of thinkinator. I still really like R and use it every day, but it’s not a great choice for a general-purpose programming language. I wanted to adopt a language that runs on the JVM (Java, Scala, Clojure, Groovie, et al) that’s fun to use, but deep. Clojure is tempting — I love Lisp — but ultimately, Scala won out. I can highly recommend it!
Screen Shot 2013-09-06 at 10.29.37 PM

Over the years, two things have kept me from adopting Java: 1) Java makes you do too much work and has too many seams — the seams hurt my head — and 2) the Java tool chain is fussy and hard to get right. Scala totally eliminates the first problem: it’s Java done right. Unfortunately, the second problem remains, so I’m writing this posting to document how I got Scala working on a Mac, with Eclipse and SBT (Simple Build Tool) in a way that everything works from Eclipse and also that I can generate JAR files that work as well. (This last part was not easy.)

I assume you are comfortable with the Mac/UNIX command line, since you’re going to be programming. I created a /usr/local directory years ago and am not sure if it exists on a stock Mac system. I put all of the executables for Scala, sbt, etc, in /usr/local.

First, install Scala, which currently is up to 2.10.2: go to Scala.org and press the “Download” button. I installed it into /usr/local/scala and edited ~/.profile to add /usr/local/scala/bin to the end of PATH and to create SCALA_HOME=/usr/local/scala. Remember to

source ~/.profile

when you’re done.

Second, install sbt: go to Scala-sbt.org and follow the UNIX instructions under Manual Installation. I chose to put the files in /usr/local/bin to keep everything together. Ignore the Mac instructions which won’t (as of this writing) install the latest version. The latest version of sbt has broken the .sbt directory up into per-version subdirectories, and documentation and online postings do not properly reflect this. Fire up sbt to create the appropriate directory, then exit.

Now cd ~/.sbt and you’ll see a directory 0.13 (the latest version). Then:

mkdir 0.13/plugins
cat > 0.13/plugins/plugins.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.3.0")

^D

where “^D” is control-D, which signals an end-of-file.

Third, let’s create a Scala project that sbt will like. Choose a directory where you’ll do your development. For example, I use ~/Documents/Development/ScalaProjects. Within that directory, create a helloWorld directory, where we’ll put our hello world program. The project directory layout that sbt likes is documented here, create that hierarchy.

Then edit the file build.sbt in the helloWorld directory and add:

name := "helloWorld"

version := "1.0"

scalaVersion := "2.10.2"

mainClass in (Compile, packageBin) := Some("HelloWorld")

Note that you must put blank lines between each line, and after the last line.

Let’s put a hello world program into a file in the project. In the helloWorld directory type:

cat > src/main/scala/helloWorld.scala
object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world!")
    }
  }
  ^D

Still in the helloWorld directory, start sbt and at its prompt type:

eclipse with-source=true

which will download a bunch of large .jar files to implement the eclipse command, then invokes the eclipse command which makes a project directory suitable for Eclipse to open.

Fourth, install Eclipse. The easiest way to do this is to get the Scala IDE, which is Eclipse Indigo (two releases back, but the previous release was not well-received, and the latest release, Kepler, just came out this summer). Choose “Get The Bundle”. If you already have Eclipse installed, you can get the Eclipse Scala plugin and install it. (The Eclipse IDE version also comes with the Scala Worksheet plugin, which is pretty amazing in itself.)

Now, you’re ready to fire up Eclipse, and then Import... and choose General > Existing Projects Into Workspace and navigate to the helloWorld directory and choose that.

If you want to run the hello world from Eclipse, right-click on the project and choose Properties, choose Run/Debug Settings, create a New... setting, and in the main tab put HelloWorld as the Main class. Eclipse can’t understand the Scala main signature, so you can’t select HelloWorld from the pop-up, you have to type it by hand. Select OK and then when you run, use that launch configuration. You should see “Hello world!” in Eclipse’s Console. With a more complicated Scala main that accepts command line arguments, you can add those into the Run/Debug setting, too.

If you want to create a jar file that can be run independently of Eclipse, go to Run > External Tools > External Tool Configurations and in the Main tab, under Location fill in where you put the sbt script file (in my case, /usr/local/bin/sbt), under Working Directory enter ${project_loc}, and under argument enter packageBin. Give it the name sbt packageBin and save it:

Screen Shot 2013-09-06 at 9.55.43 PM

By selecting that tool, you’ll create an executable in target/scala-2.10/helloWorld_2.10-1.0.jar that can be run with the scala command (which sets up paths properly for you). If you’re in the helloWorld directory, typing:

scala target/scala-2.10/helloWorld_2.10-1.0.jar

at the command line should also print “Hello world!”. The name of the jar file is obviously built from the build.sbt command’s keywords, and you can see how easy it would be to have a build.sbt that has a different version of helloWorld or of Scala that would create an output file of a different name so you could maintain separate versions.

The order I described things may seem a little strange: why did I have you cat the helloWorld program instead of using Eclipse to type it in? Of course, I had already downloaded Eclipse and created my project entirely in Eclipse and figured out the Run/Debug settings, but simply could not get it to work outside of Eclipse. I tried various workarounds (like creating a java file with a java mail that called the Scala main) but nothing worked. Fortunately, sbt can create the proper manifest file that tells java (called from the scala command) where your main is. It seemed easier to not have you close and relocate projects, which would have taken more explanation.

Hope that helps. It’s a wall of text, and I’ll come back and try to break it up and add more screen captures, but I wanted to get it all down while I still remembered what I did.

Advertisements

One thought on “Scala + Eclipse + sbt on a Mac

  1. Pingback: #HelloWorld « itech104 Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s