scalashebangscalac

Shebangs in scalac code


The scala interpreter allows shebangs, but strangely, scalac borks on them. Are there any tricks like these to get around this flaw?


Solution

  • #! in scala must be closed with !#. For example:

    #!/bin/bash
    script_dir=$(cd $(dirname "$0") >/dev/null; pwd -P)
    classes_dir=${script_dir}/build/classes
    src_dir=${script_dir}/src
    
    mkdir -p ${classes_dir}
    scalac -d ${classes_dir} $(find ${src_dir} -name '*.scala')
    
    exec /usr/bin/scala -classpath ${classes_dir} "$0" "$@"
    !#