How to compile Scala code with ‘scalac’ and run it with ‘scala’

This is an excerpt from the Scala Cookbook. This is Recipe 14.5, “How to compile Scala code with scalac and run it with scala.”

Problem

Though you normally use the Simple Build Tool (SBT) to build Scala applications, you may want to use more basic tools to compile and run small test programs, in the same way you might use javac and java with small Java applications.

Solution

Compile programs with scalac, and run them with scala. For example, given a Scala source code file named Hello.scala:

object Hello extends App {
    println("Hello, world")
}

Compile it from the command line with scalac:

$ scalac Hello.scala

Then run it with scala:

$ scala Hello
Hello, world

Discussion

Compiling and executing classes is basically the same as Java, including concepts like the classpath. For instance, if you have a class named Pizza in a file named Pizza.scala, it may depend on a Topping class:

class Pizza (var toppings: Topping*) {
    override def toString = toppings.toString
}

Assuming that the Topping class is compiled to a file named Topping.class in a subdirectory named classes, compile Pizza.scala like this:

$ scalac -classpath classes Pizza.scala

In a more complicated example, you may have your source code in subdirectories under a src folder, one or more JAR files in a lib directory, and you want to compile your output class files to a classes folder. In this case, your files and directories will look like this:

./classes
./lib/DateUtils.jar
./src/com/alvinalexander/pizza/Main.scala
./src/com/alvinalexander/pizza/Pizza.scala
./src/com/alvinalexander/pizza/Topping.scala

The Main.scala, Pizza.scala, and Topping.scala files will also have package declarations corresponding to the directories they are located in, i.e.:

package com.alvinalexander.pizza

Given this configuration, to compile your source code files to the classes directory, use the following command:

$ scalac -classpath lib/DateUtils.jar -d classes " src/com/alvinalexander/pizza/*

Assuming Main.scala is an object that extends App, Pizza.scala is a regular class file, and Topping.scala is a case class, your classes directory will contain these files after your scalac command:

./classes/com/alvinalexander/pizza/Main$.class
./classes/com/alvinalexander/pizza/Main$delayedInit$body.class
./classes/com/alvinalexander/pizza/Main.class
./classes/com/alvinalexander/pizza/Pizza.class
./classes/com/alvinalexander/pizza/Topping$.class
./classes/com/alvinalexander/pizza/Topping.class

Once the files have been compiled in this manner, you can run the application like this:

$ scala -classpath classes:lib/DateUtils.jar com.alvinalexander.pizza.Main

As you can imagine, this process gets more and more difficult as you add new classes and libraries, and it’s strongly recommended that you use a tool like SBT, Maven, or Ant to manage your application’s build process. The examples shown in this recipe are shown for the “one off ” cases where you might want to compile and run a small application or test code.

For other useful command-line options, see the manpages for the scalac and scala commands.