Scala/Ammonite: How to load/import a jar file into the REPL

TIL that you can import/load a JAR file into the Scala Ammonite REPL using this import command:

import $cp.foo.`simpletest_3.0.0-0.2.0.jar`

That command assumes that the JAR file is named simpletest_3.0.0-0.2.0.jar, and it’s in a subdirectory named foo.

It took me a little while to figure out every part of that, including the backtick incantation, so I thought I’d share that here. FWIW, once I import my JAR file I can then use it like this:

import com.alvinalexander.simpletest.SimpleTest._
isTrue(true)

My comical previous attempts

As a bit of a laugh at myself, my previous, comical attempts to get this working looked like this:

import java.nio.file.{Path, Paths}
val currDirAbsPath = Paths.get(".").toAbsolutePath().normalize().toString
val jarPath = java.nio.file.Paths.get(currDirAbsPath, "foo", "simpletest_3.0.0-0.2.0.jar")
val p = ammonite.ops.Path(jarPath)
interp.load.cp(p)
import com.alvinalexander.simpletest.SimpleTest._
isTrue(true)

While I say that’s comical, atm there aren’t any good docs on how to import a JAR file into the Ammonite REPL, which is another reason for sharing this.