Short source code examples

I used the following code to convert an array of Scala objects on the server into a JSON string that I could return back to the client. So it's going from a MongoDB collection to an array of Scala objects through Gson to become a JSON string.

I accidentally created some MongoDB collections within my database named "stickynotes" that I didn't want. All I wanted was a collection named notes, but I was fooling around and accidentally created some other collections. Here's how I deleted those collections using the mongo command line. (My commands are shown in bold.)

Here's an example of how I deserialize an array of objects encoded in JSON, in this case using Scalatra and Scala:

I was able to get Gson to work with Scala and Scalatra very easily. Here's a sample Scalatra POST method where I receive a JSON string and convert it to a StickyNote case class object:

When working on a Scalatra project, this is how I start the Jetty server using SBT:

# (1) start sbt
$ sbt

# (2) start the jetty server at the sbt command line
> container:start

There is supposed to be a way to get this to be a "continuous compile", so you don't always have to stop and start Jetty, but I haven't been able to get that to work properly.

This is a simple way to center a JFrame in Java:


Scala doesn't have any special email handling facilities, so I just use my normal Java approach to send mail.

Here's the source code for my Scala 'send email' class. It's basically just a port of my previous Java 'send email' class, and requires the Java Mail API and Activation Framework jar files:

I took a couple of hours to write some Scala scripts to email me some Twitter information a couple of times a day, and while creating those scripts I created a simple Linux shell script to compile my Scala classes, while also including my library dependencies. Here's the Linux shell script I created and named

Depending on your needs there are a couple of ways to get the current working directory in a Scala application.

1) Using System.getProperty

The most obvious/direct approach is to use the Java System.getProperty method, passing in "user.dir" as a parameter:

I wrote a naive servlet application, where I'm printing to System.out and System.err, and when I use Jetty, I'm not seeing any of this output. According to the URL I've linked to, this should work with the following syntax. Here's what they say, and the Jetty startup command they recommend:

You can use etc/jetty-logging.xml to take all System.out and System.err output (from any source) and route it to a rolling log file. To do so, include etc/jetty-logging.xml on Jetty startup.

java -jar start.jar etc/jetty-logging.xml

How to create a JButton without a border:


I just tested this on a new Java Swing app, and it works as advertised, removing the JButton border.


In Java you often use ".class" on a class or getClass on an object to do some things, usually some sort of reflection work. The rough equivalent of that in Scala is the classOf operator, as shown in this code:

While working on a Scalatra web service recently, I decided to use JSON pass my data into a REST POST method, and within that POST method I decided to turn the JSON String back into an object using the Google Gson library. This was a little tricky, and kept throwing an exception until I realized that the Scalatra params.get method was returning a Scala Some reference instead of the plain String I was expecting.

I'm not an Android expert, but I wanted to use the Android Holo Light theme, and changing my <application> tag in my Android application manifest file to look like this definitely switched my Android app from having a dark background to a light background:


That last line of code where I set the "theme" is what did the trick for me.

Here's a quick example of how to create a JSON object (String, technically) using the Google Gson library and Scala. Note that this is very easily converted to Java:

This Scalatra POST method shows one way to handle HTTP POST data. It shows that you can access POST parameters using the Scalatra params function:

I created this Scala class as a way to test an HTTP POST request to a web service. Although its written in Scala, it uses the Apache HttpClient Java libraries. I got the NameValuePair code from the URL I've linked to.

If you have a Scalatra POST method like this:

post("/foo") {
  // do stuff here

you can access the data that's sent to your POST function by using the implicit request object, like this:

val content = request.body

Then you can operate on that content as desired.

I just generated this example build.sbt SBT file when experimenting with Scalatra. I manually added the MongoDB Casbah dependency line.

First, I went to the URL shown.

Second, I updated SBT to the latest 0.11.x version.

Then I added this line:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.0.0-M3")

to the file ~/.sbt/plugins/build.sbt (Note that there is an error in there docs, with an extra "}".)

I was then able to run this command: