repl

How to start a Scala REPL session inside SBT (Simple Build Tool)

Scala FAQ: How do I start a Scala REPL session from SBT (i.e., inside the Simple Build Tool)?

Answer: Use console or consoleQuick:

  • Type console to start a REPL session from inside SBT. This (a) compiles your Scala project and then (b) starts a REPL session.
  • Type consoleQuick if you don’t want to compile your project before starting a REPL session inside of SBT.

The console command process looks like this:

> sbt
[info] Loading project definition from /Users/al/Projects/Cats101/project
[info] Set current project to Cats101 (in build file:/Users/al/Projects/Cats101/)

> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> _

If you wanted to see how to start a Scala REPL session from inside SBT, I hope this helps.

How to run shell commands from the Scala REPL

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.4, “How to run a shell command from the Scala REPL.”

Problem

You want to be able to run a shell command from within the Scala REPL, such as listing the files in the current directory.

Solution

Run the command using the :sh REPL command, then print the output. The following example shows how to run the Unix ls -al command from within the REPL, and then show the results of the command:

How to clear/reset a Scala REPL session (without having to kill the session)

A cool feature of the Scala REPL is that you can reset/clear a REPL session. To do so, just issue the :reset command, like this:

scala> :reset
Resetting interpreter state.
Forgetting this session history:

Assuming that you already have at least a little history in your REPL session, the :reset command will show you everything that it dumps, so the full output looks more like this:

How to compile, run, and package a Scala project with SBT

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.2, “How to compile, run, and package a Scala project with SBT.”

Back to top

Problem

You want to use SBT to compile and run a Scala project, and package the project as a JAR file.

Back to top

Solution

Create a directory layout to match what SBT expects, then run sbt compile to compile your project, sbt run to run your project, and sbt package to package your project as a JAR file.

Table of Contents

  1. Problem
  2. Solution
Back to top

Scala: How to add Jar files and classes to the REPL Classpath

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.3, “How to add Jar files and classes to the REPL Classpath.”

Problem

You want to add individual classes or one or more JAR files to the REPL classpath so you can use them in a REPL session.

How to paste and load blocks of code in the Scala REPL

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.2, “How to paste and load blocks of code in the Scala REPL.”

Problem

You want to experiment with some code in the Scala REPL, and typing it in or trying to paste it into the REPL won’t work.

Getting started with the Scala REPL (command-line shell)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.1, “How to get started with the Scala REPL.”

Back to top

Problem

You want to get started using the Scala REPL, including understanding some of its basic features, such as tab completion, starting the REPL with different options, and dealing with errors.

Back to top

Solution

To start the Scala REPL, type scala at your operating system command line:

Table of Contents

  1. Problem
  2. Solution
Back to top