Short source code examples

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

How to get the current working directory in a Scala application:

There may be other ways to do this, but I just use the Java current directory approach:

def getCurrentDirectory = new "." ).getCanonicalPath

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:

How to find all files that have been added within the last day on a Mac OS X system: This command doesn't seem to be as accurate as the Unix find command, but it seems to be a bit faster:

mdfind 'kMDItemFSCreationDate >= $time.yesterday' | grep -v Cache | grep ".jar"

I just tried to use it to find some files that were recently downloaded by Maven, but it didn't actually work very well. That being said, I hope that a variation of this mdfind command will be helpful.

You can use Java Collections in a Scala app by using this import statement:

import scala.collection.JavaConversions._

I just tested the following Scala application that gets a Java collection from the MongoDB Java driver, and I can confirm that this works:

How to start MongoDB with a different database directory:

bin/mongodb --dbpath /Users/Al/data/mongodatabases



The Android API makes extensive use of what I thought were "static inner classes", but apparently the correct term is "static nested class". Here's a description of the differences between static and non-static nested inner classes from the URL shown:

"A non-static nested class (or 'inner class') has full access to the members of the class within which it is nested.

Using a parallel map to transform a collection of String to all-uppercase