Scala HelloWorld Program using SBT [Simple Build Tool]

SBT is a build tool for Scala, Java, and more. It requires Java 1.6 or later.

Installation of SBT

$ mkdir $HOME/bin

$ wget -O $HOME/bin/sbt-launch.jar

$ cat << EOM >> $HOME/bin/sbt
SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
java $SBT_OPTS -jar $HOME/bin/sbt-launch.jar "$@"

$ chmod u+x $HOME//bin/sbt

$ echo export PATH=$PATH:$HOME/bin >>$HOME/.bashrc

Create Scala Hello World Program

$ mkdir scala-demo

$ cd scala-demo

$ echo 'object Hi { def main(args: Array[String]) = println("Hello World! Scala") }' > HelloWorld.scala
$ sbt run

output:>> Hello World! Scala

Moreover sbt works purely by convention. sbt will find the following automatically:
  • Sources in the base directory
  • Sources in src/main/scala or src/main/java
  • Tests in src/test/scala or src/test/java
  • Data files in src/main/resources or src/test/resources
  • jars in lib
Common SBT commands
Here are some of the most common sbt commands. For a more complete list, see Command Line Reference.
  • clean Deletes all generated files (in the target directory).
  • compile Compiles the main sources (in src/main/scala and src/main/java directories).
  • test Compiles and runs all tests.
  • console Starts the Scala interpreter with a classpath including the compiled sources and all dependencies. To return to sbt, type :quit, Ctrl+D (Unix), or Ctrl+Z (Windows).
  • run * Runs the main class for the project in the same virtual machine as sbt.
  • package Creates a jar file containing the files in src/main/resources and the classes compiled from src/main/scala and src/main/java.
  • help Displays detailed help for the specified command. If no command is