Run scala console with play 2.2 jars

2019-07-26 15:13发布

问题:

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

回答1:

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/'


回答2:

@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" ":"`