You can run a scala script as a linux shell script:
#!/bin/sh
exec scala "$0" "$@"
!#
println("Hello")
In one such script I need to load classes from a group of jars (which happen to be in the same directory as the script). If this were the REPL I could use :jar, but that’s not available in script mode.
I’m trying to set the -classpath parameter:
#!/bin/sh
exec scala -classpath '.:./*.jar' "$0" "$@"
!#
import javax.media.jai.{JAI, RenderedOp}
but the compiler just can’t find the classes:
error: object media is not a member of package javax
import javax.media.jai.{JAI, RenderedOp}
^
How do I include these jars?
For some reason, the glob (
*.jar) wasn’t working. I was able to get the script running by putting in all the libraries by hand:I don’t know why the glob isn’t working though.
Note that in this case I don’t have the
.in the classpath because the script itself is provided as an argument. In many cases though you will need to include it:Based on this helpful post, I have a script header that pulls in every jar in a
libfolder, even if the script (or the folder it’s in) are symlinks.$0into its actual location on disk, expanding symlinks./libcpvariable with all the jars separated by: