Bill Agee's blog

🤔 Reflections on test infrastructure, with a twist of user empathy.

Testing Java snippets with Vim and GroovyConsole

For instructional purposes (either when experimenting on your own, or when demonstrating code to others) it's always useful to be able to run snippets of code in a REPL, or a similar environment allowing fast turnaround in the edit/compile/run cycle.

When using Java, other IDEs offer ways to get REPL-like behavior, but what if you don't want to use a traditional Java IDE?

Perhaps you just want to demonstrate a trivial bit of code without much overhead.

In that situation, a couple of nice options for Java are:

  • Use GroovyConsole as a Java REPL
  • Edit your code in Vim, and compile and run it without leaving the editor

1. Using GroovyConsole

If you're on a Mac, GroovyConsole can be installed via the homebrew groovy formula (or, just get it from

brew install groovy

Launch GroovyConsole with this command:


Then simply type in a code snippet and run it with <Command-R> (or on Windows, <CTRL-R>):

groovyConsole screenshot

2. Using Vim as an improvised Java IDE

foo dot java

First, launch vim and write a small program - for example,

running javac from vim

Then, compile your program without leaving vim by passing the file open in your vim buffer to javac, using the :! command sequence and %

ENTER prompt from vim

If all goes well, you'll temporarily be dropped to the shell, with no visible errors, and get prompted to press ENTER to continue back to vim:

running Foo.class in java

Back in the editor, use :!java Foo to invoke the Java class file you just created with javac:

Foo.class output

Finally, you'll see your program's output in the console.

For further fun, try compiling with javac -g %, then launch your class file with jdb Foo to debug your program from within vim.

Also, you might consider taking the javacomplete omni-completion plugin for a spin.