LinuxCommandLibrary

scala

Run the Scala compiler and REPL

TLDR

Start a REPL (interactive shell)

$ scala
copy

Start the interpreter with a dependency in the classpath
$ scala -classpath [filename.jar] [command]
copy

Execute a Scala script
$ scala [script.scala]
copy

Execute a .jar program
$ scala [filename.jar]
copy

Execute a single Scala command in the command-line
$ scala -e [command]
copy

SYNOPSIS

scala [options] [scriptfile | classname] [args...]
scala [options] (to start REPL)

PARAMETERS

-classpath <path> | -cp <path>
    Specifies the user class path for loading classes and resources. This is crucial for including external libraries or your own compiled code.

-D<property>=<value>
    Sets a Java system property. Useful for configuring applications that rely on system properties (e.g., -Dmy.setting=value).

-J<flag>
    Passes a raw flag directly to the underlying Java runtime. This is used for JVM tuning, such as setting memory limits (e.g., -J-Xmx1G).

-X<option>
    Passes an option to the Scala runtime, often for experimental or advanced features/diagnostics.

-i <file>
    Reads and executes commands from the specified file, then enters the REPL. Useful for loading common definitions or setup code into the interactive session.

-e <command>
    Executes the given command string as a Scala script and then enters the REPL. Useful for one-liners before interacting.

-howtorun:script
    Treats arguments as a Scala script file to be executed. This is the default behavior when a file argument is provided.

-howtorun:object
    Treats the first argument as the name of a top-level Scala object (containing a main method) to run.

-howtorun:jar
    Treats the first argument as a JAR file to be executed, typically one with a Main-Class entry in its manifest.

-howtorun:repl
    Forces scala to enter the interactive REPL. This is the default when no arguments are given.

-version
    Prints the Scala product version and exits.

-help
    Displays a comprehensive list of available options and their descriptions.

DESCRIPTION

The scala command is the primary command-line interface for executing Scala programs and scripts, as well as initiating the interactive Scala Read-Eval-Print Loop (REPL).

Scala is a powerful multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It runs on the Java Virtual Machine (JVM) and is interoperable with Java code.

When invoked without arguments, scala launches the REPL, providing an immediate environment for experimenting with Scala code, testing snippets, and learning the language interactively. When provided with a script file (e.g., with a .scala extension) or the name of a compiled Scala object (class file), it executes the specified program. It acts as an interpreter for Scala source files or as a launcher for compiled bytecode, relying on the underlying Java runtime environment.

CAVEATS

scala requires a Java Development Kit (JDK) or Java Runtime Environment (JRE) to be installed and accessible in the system's PATH, as it relies heavily on the JVM.

Running Scala scripts directly with scala can incur a noticeable startup overhead due to JVM initialization, especially for small scripts. For compiled applications, this overhead is typically paid once at launch.

Memory usage can be significant, particularly for complex applications or prolonged REPL sessions, as the JVM and Scala runtime consume considerable resources.

THE REPL (READ-EVAL-PRINT LOOP)

The Scala REPL is an invaluable tool for learning and prototyping. It allows users to type Scala expressions or statements directly into the console, which are then immediately evaluated, and the result is printed. This interactive feedback loop significantly speeds up development and experimentation compared to traditional compile-run cycles.

EXECUTION MODES

The scala command can execute code in several ways:
1. Interpreting a script file:

scala MyScript.scala

2. Running a compiled class:
scala MyPackage.MyObject
(requires MyObject.class to be on the classpath)
3. Launching a JAR file:
scala -howtorun:jar myapp.jar

4. Interactive REPL:
scala
(without arguments)
Understanding these modes is key to effectively using the scala command for different development and deployment scenarios.

HISTORY

The Scala language and its associated tools, including the scala command, were created by Martin Odersky at EPFL (École Polytechnique Fédérale de Lausanne) in Switzerland. The first public release of Scala was in 2004. The scala command has been a fundamental component of the Scala distribution since its inception, providing immediate access to the interpreter and the ability to execute Scala programs directly from the command line. Its design reflects Scala's core philosophy of blending object-oriented and functional programming paradigms on the robust Java Virtual Machine.

SEE ALSO

java(1), scalac(1), sbt(1)

Copied to clipboard