After years of Python programming, I've been spoiled by always having a REPL available when I want to test something out. As I spend more and more time in Java, I find myself missing a REPL. Well, no more. I'm happy to report that I've finally found a REPL I like using to test out libraries when I'm working in Java: the Scala REPL.
Today I needed to send some JMS messages to ActiveMQ. Instead of firing up eclipse, creating a maven project to get the dependencies, and then writing some Java code, I used sbt and Scala to create a REPL where I could easily send JMS messages.
Create an sbt project
The first step is to create an sbt project. This is as simple as creating a directory with a
build.sbt file in it. My example
build.sbt file pulls in two library dependencies that I want to have available in the REPL.
name := "jmstest" scalaVersion := "2.9.1" libraryDependencies ++= Seq( "org.apache.activemq" % "activemq-core" % "5.5.1", "org.slf4j" % "slf4j-simple" % "1.6.4" )
Run the REPL
Running the REPL is as simple as running
sbt console from the newly created sbt project. From there, it's off to the races:
import javax.jms._ import org.apache.activemq.ActiveMQConnectionFactory val factory = new ActiveMQConnectionFactory("tcp://localhost:61616") val connection = factory.createConnection val session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE) val destination = session.createTopic("jms.test") val producer = session.createProducer(destination) val message = session.createMapMessage message.setString("command", "basic_setup") message.setBoolean("do_stuff", false) producer.send(message)
That's all there is to it. Scala, with the help of sbt, makes a great REPL for testing out Java libraries.