I have a Scala project that contains both Scala classes/objects as well as scripts. I am trying to run the scripts (that use the classes/objects to perform various actions) from command line. The way I am looking to do this is to create a shell that would be passed as argument the name of the script, as well as the arguments that the script itself takes.
My folder structure is as follows:
runner.sh -> script I am trying to implement to run the Scala scripts
bin/ -> script I am trying to implement to run the Scala scripts
lib/ -> -> rest of scala project, packed as multiple jars
I am currently creating a variable LIB_CLASSPATH
in the shell script that contains the contents of the lib/
folder and I am trying to run my scrips as follows:
java -cp $LIB_CLASSPATH ./bin/<name_of_script.scala> <parameters of the script>
I always get back the same error:
Could not find or load main class: ..lib/<name of jar>
-- <name of jar> is actually the first jar in the lib/ folder
I could use some guidance :)
I would recommend using sbt native packager. See:
https://github.com/sbt/sbt-native-packager
You add this line to build.sbt:
And define a main class:
To run:
It create startup script located in a place like this:
Here is an example of how I used the script:
If you want to run another class you can do this:
Here's a script that will do what you want. It's based on a script I use, but stripped down for clarity:
The easiest way to use this script is to place it in your PATH, and then add the following hash-bang line to the top of your scala script. Assuming you call this script 'scalaRunner', here's a standalone working scala script:
Here's one that's slightly more elaborate:
If you import anything, just make sure all required jars are in the REQUIRED_JAR_DIRECTORIES.
In practice, you may want something more flexible than this script, especially with regard to specifying where required jars reside.
You can use
Ammonite
(written by @lihaoyi).It's a combination of a better Scala REPL, Shell, OPs library, and Script runner. See the docs here - http://www.lihaoyi.com/Ammonite/
10 min talk showing how is it better for scripting & shell - https://www.youtube.com/watch?v=wk2-ZsQU358
I think it can be a good starting point as well. Talk was given by me :)