Run scala console with play 2.2 jars

2019-07-26 15:24发布

I saw people using play apis in the scala console. How can I import multiple play's jars to scala console?

https://stackoverflow.com/a/17684559/772481

I tried this command, but it didn't work.

$ scala -cp /usr/local/Cellar/play/2.2.0/libexec/repository/local/com.typesafe.play/play_2.10/2.2.0/jars/*.jar

2条回答
孤傲高冷的网名
2楼-- · 2019-07-26 15:53

@aleksey's answer works for me. I also find another way to do it.

Go to your project target folder

$ cd <your play project>/target/universal/stage/lib

Now, run this command.

$ scala -cp `ls -1 | tr "\\n" ":"`
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-26 15:59

Your approach was right. scala REPL understands all scalac options, so scala -cp is the right option. However, you'll have to list all jars separately in the classpath separated by colon on Unix.

If you are lazy like me and don't want to spell out all jar files you can use something like this to build the classpath:

ls -1 ~/.ivy2/cache/com.typesafe/config/jars/config-1.0.* | tr '\n' ':' | sed 's/:$/\n/'

Produces:

/home/alex/.ivy2/cache/com.typesafe/config/jars/config-1.0.0.jar:/home/alex/.ivy2/cache/com.typesafe/config/jars/config-1.0.2.jar

Use it in your original command:

scala -cp "$(ls -1 /usr/local/Cellar/play/2.2.0/libexec/repository/local/com.typesafe.play/play_2.10/2.2.0/jars/*.jar | tr '\n' ':' | sed 's/:$/\n/')"

If you want to avoid specifying a directory use find instead of ls:

find ~/.ivy2/cache/com.typesafe/config/jars/ -name "*.jar" -exec echo {} \; | tr '\n' ':' | sed 's/:$/\n/'
查看更多
登录 后发表回答