How to add a new jar file to the Scala REPL classpath (interactive command line)

Scala REPL FAQ: How do I add a Jar file to the Scala REPL classpath? (The Scala REPL is the interactive command line you get if you just type scala at your command line.)

To add a new jar file to the Scala REPL classpath (interactive command line classpath), use the :require command at the command line, like this:

scala> :require myjar.jar

After you do this, you should see a reply from the REPL like this:

Added '/Users/Al/tests/myjar.jar'. Your new classpath is: ".:/Users/Al/tests/myjar.jar"

At this time you can begin using the classes in your jar (though you'll have to import them, as usual).

Adding a jar file to the classpath when you start the Scala interpreter

I usually find that I need to add a jar file to the Scala REPL after I've been running the REPL, but if you happen to know that you're going to need to add a jar file to the classpath when you start the interpreter, you can do so like this:

$ scala -cp junit-4.8.1.jar

As you can see, this approach uses the usual "-cp" classpath option that is used by the scalac and javac commands.

Scala REPL help

I learned about the Scala REPL :require command by looking at the help from within the REPL. Here's what that help looks like with my current version of Scala, version 2.12.1:

scala> :help

All commands can be abbreviated, e.g., :he instead of :help.
:edit <id>|<line>        edit history
:help [command]          print this summary or command-specific help
:history [num]           show the history (optional num is commands to show)
:h? <string>             search the history
:imports [name name ...] show import history, identifying sources of names
:implicits [-v]          show the implicits in scope
:javap <path|class>      disassemble a file or class name
:line <id>|<line>        place line(s) at the end of history
:load <path>             interpret lines in a file
:paste [-raw] [path]     enter paste mode or paste a file
:power                   enable power user mode
:quit                    exit the interpreter
:replay [options]        reset the repl and replay all previous commands
:require <path>          add a jar to the classpath
:reset [options]         reset the repl to its initial state, forgetting all session entries
:save <path>             save replayable session to a file
:sh <command line>       run a shell command (result is implicitly => List[String])
:settings <options>      update compiler options, if possible; see reset
:silent                  disable/enable automatic printing of results
:type [-v] <expr>        display the type of an expression without evaluating it
:kind [-v] <expr>        display the kind of expression's type
:warnings                show the suppressed warnings from the most recent line which had any

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.