/scalive

Connect a Scala REPL to running JVM processes without any prior setup

Primary LanguageJavaMIT LicenseMIT

This tool allows you to connect a Scala REPL console to running Oracle (Sun) JVM processes without any prior setup at the target process.

View demo video on YouTube

Download

Download and extract scalive-1.5.zip, you will see:

scalive-1.5/
  scalive
  scalive.cmd
  scalive-1.5.jar

  scala-library-2.10.4.jar
  scala-compiler-2.10.4.jar
  scala-reflect-2.10.4.jar

  scala-library-2.11.4.jar
  scala-compiler-2.11.4.jar
  scala-reflect-2.11.4.jar

scala-library, scala-compiler, and scala-reflect of the appropriate version will be loaded to your running JVM process, if they have not been loaded. The REPL console needs these libraries.

For example, your process has already loaded scala-library 2.10.4 by itself, but scala-compiler and scala-reflect haven't been loaded, Scalive will automatically load their version 2.10.4.

If none of them has been loaded, Scalive will load version 2.11.4.

For convenience, Scala 2.10.4 and 2.11.4 JARs are preincluded. If your process is using a different Scala version, you need to manually download the corresponding JARs from the Internet and save them in the same directory as above.

Usage

Run the shell script scalive (*nix) or scalive.cmd (Windows).

To see a list of running JVM processes and their process IDs:

scalive

To connect a Scala REPL console to a process:

scalive <pid>

How to add your own JARs

Scalive only automatically adds scala-library.jar, scala-compiler.jar, scala-reflect.jar, and scalive.jar to the system classpath. If you want to load additional classes in other JARs, first run these in the REPL console to add the JAR to the system class loader:

val cl         = ClassLoader.getSystemClassLoader.asInstanceOf[java.net.URLClassLoader]
val searchDirs = Array("/dir/containing/the/jar")
val jarbase    = "mylib"  // Will match "mylibxxx.jar", convenient when there's version number in the file name
scalive.Classpath.findAndAddJar(cl, searchDirs, jarbase)

Now the trick is just quit the REPL console and connect it to the target process again. You will be able to use your classes in the JAR as normal:

import mylib.foo.Bar
...

Note that :cp doesn't work.

How Scalive works

Scalive uses the Attach API to tell the target process to load an agent.

The agent then creates a TCP server to let the Scalive process interact with the target process. The Scalive process acts as a client.

See also liverepl, a similar REPL console for Clojure.

Known issues

For simplicity and to avoid memory leak when you attach/detach many times, Scalive only supports processes with only the default system class loader, without additional class loaders (Ex: normal standalone JVM processes, like Play or Xitrum in production mode).

Processes with multiple class loaders like Tomcat or SBT are not supported (with SBT, you already has the SBT console, so it's not a big deal).

These features will be added in the future: