To run the examples provided in this book, we need to install Scala and SBT. Depending on your operating system, different steps need to be taken.
Before we can install SBT and Scala, we first need to install Java. Scala requires at least a Java Runtime version of 1.6 or higher. If you haven't installed Java on your system yet, follow the instructions at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
Once you have Java installed, installing Scala and SBT is just as easy. To install Scala, just go to http://www.scala-lang.org/download/ and download the binaries for your system. To install SBT, you can follow the instructions at http://www.scala-sbt.org/download.html.
To check whether everything is installed, run the following commands in a terminal:
Joss-MacBook-Pro:~ jos$ java -version java version "1.8.0_40" Java(TM) SE Runtime Environment (build 1.8.0_40-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode) Joss-MacBook-Pro:~ jos$ scala -version Scala code runner version 2.11.6 -- Copyright 2002-2013, LAMP/EPFL Joss-MacBook-Pro:~ jos$ sbt -v [process_args] java_version = '1.8.0_40' # Executing command line: java -Xms1024m -Xmx1024m -XX:ReservedCodeCacheSize=128m -XX:MaxMetaspaceSize=256m -jar /usr/local/Cellar/sbt/0.13.8/libexec/sbt-launch.jar [info] Set current project to jos (in build file:/Users/jos/)
To exit SBT, hit Ctrl + C.
Now that you've got Java, Scala, and SBT installed, we can run the examples. You can, of course, run the examples from your IDE (see the next section on how to set up IntelliJ IDEA and Eclipse), but often, using SBT directly is just as easy. To run the examples, take the following steps:
Open a terminal and go to the directory where you've extracted the source ZIP file or cloned the repository.
To test the configuration, we've created a simple
HelloWorld
example. From the console, executesbt runCH01-HelloWorld
:Joss-MacBook-Pro:rest-with-scala jos$ sbt runCH01-HelloWorld [info] Loading project definition from /Users/jos/dev/git/rest-with-scala/project [info] Set current project to rest-with-scala (in build file:/Users/jos/dev/git/rest-with-scala/) [info] Compiling 2 Scala sources to /Users/jos/dev/git/rest-with-scala/chapter-01/target/scala-2.11/classes... [info] Running org.restwithscala.chapter1.HelloWorld SBT successfully ran HelloWorld, configuration seems ok! Press <enter> to exit. [success] Total time: 18 s, completed Jun 13, 2015 2:34:41 PM Joss-MacBook-Pro:rest-with-scala jos$
You might see a lot of output when the various dependencies are loaded, but after a while, you should see the message,
SBT successfully ran HelloWorld, configuration seems ok!
.All the examples in this book wait for user input to terminate. So, once you're done playing around with the example, just hit Enter to terminate the running program.
In each chapter, we'll see the sbt
command we need to execute. If you want to know all the examples you can run, you can also run the sbt
alias
command, which generates the following output:
Joss-MacBook-Pro:rest-with-scala jos$ sbt alias [info] Loading project definition from /Users/jos/dev/git/rest-with-scala/project [info] Set current project to rest-with-scala (in build file:/Users/jos/dev/git/rest-with-scala/) runCH01-HelloWorld = ; chapter01/runCH01HelloWorld runCH01-EchoServer = ; chapter01/runCH01EchoServer
Besides running the examples directly from the command line, it is also possible to run them from an IDE. In the following section, we'll see how to import the examples in IntelliJ IDEA and Eclipse.