从shell脚本运行scala脚本 [英] Run scala script from shell script
问题描述
我有一个Scala项目,其中包含Scala类/对象以及脚本。我正在尝试从命令行运行脚本(使用类/对象执行各种操作)。我正在寻找的方式是创建一个外壳,该外壳将作为脚本名称以及脚本本身采用的参数作为参数传递。
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.
我的文件夹结构如下:
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
我当前正在创建变量 shell脚本中的LIB_CLASSPATH
包含 lib /
文件夹的内容,我正在尝试按以下方式运行脚本:
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 :)
推荐答案
您可以使用 Ammonite
(由@lihaoyi编写)。
You can use Ammonite
(written by @lihaoyi).
它是更好的Scala REPL,Shell,OPs库和脚本运行器的组合。
在此处查看文档- http://www.lihaoyi.com/Ammonite/
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分钟的演讲展示了如何更好地编写脚本&外壳- https://www.youtube.com/watch?v=wk2-ZsQU358
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 :)
这篇关于从shell脚本运行scala脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!