Compile Scala class files from the command line (a Scalac shell script that includes jar file dependencies)

I took a couple of hours to write some Scala scripts to email me some Twitter information a couple of times a day, and while creating those scripts I created a simple Linux shell script to compile my Scala classes, while also including my library dependencies. Here's the Linux shell script I created and named compile.sh:

#!/bin/sh

THE_CLASSPATH=.
for i in `ls ../lib/*.jar`
do
  THE_CLASSPATH=${THE_CLASSPATH}:${i}
done

# compile all the scala class files in the current directory,
# putting my jar file dependencies on the classpath, and writing
# the ".class" files as output to the ../bin directory:
scalac -classpath "$THE_CLASSPATH" \
       -d ../bin \
       *.scala

Note that this script does several things:

  • Includes the jar files I have in my ../lib subdirectory by putting them in my classpath.
  • Compiles the output to my ../bin directory by using the -d option of the scalac command.