Archive for the ‘Java’ Category.


I had an idea about writing a small code to animate fireflies. On Christmas break I was bored and I figured I had a few hours to play with it. I also wanted to use some tools I hadn’t used yet and figured that was a good time to do Javascript since I haven’t done much Javascript. I fired up the editor ( Sublime ) and used the browser to run / debug, Firefox and FireBug of course. There is a bit of a difference using this setup compared to writing Java code in Eclipse which I’m used to. First off you don’t get the compilation in Sublime and secondly you have to switch to another program to load and debug. Switching between the two was what annoyed me. Later I though, what if I can find a tool that is an editor and can Ftp to a server as well and runs within the browser. If on each save you can Ftp it to a sever that is serving the Javascript then I could do it all from the browser. I went looking around and fount Net2Ftp which is supposed to at least come close to the tool I’m wishing for. I will have to try it out the next time I’m coding in Javascript. The other thing that got me is that there is no good way of declaring your own classes, you have to work with prototypes and such, why that is beats me.

As you can see from the code, it’s not pretty.

If you want to see the Javascript in action click here.


Java Spring MVC 3.2.0 UnitTests

I had a bit of a fight today, I wanted to UnitTest some Java Spring MVC code that I’m using for a demo application. But after a lot of Googling, StackOverflow and hitting different documentation of Spring 3.0, 3.1 and 3.2 I finally came to an acceptable solution. Mind you a minimal sample to test MVC controller without having to go crazy with decelerations, attributes, injections and so on and so forth, you catch my drift. There has been a lot of movement from those three versions I mentioned and finally the spring UnitTesting lib has now been folded into Spring itself, this is good news.
Btw, I found the answer in the Spring MVC showcase on GitHub, where else ?

So lets take a look at what you need, bare minimum just the way I like it.

Using Maven, you will need the ref in your POM file


The controller class I’m using comes pretty much directly from creating a new MVC Spring project from Eclipse.

package us.kristjansson.springTest;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

 * Handles requests for the application home page.

public class CxHomeController
        private static final Logger logger = LoggerFactory.getLogger(CxHomeController.class);
         * Simply selects the home view to render by returning its name.

        @RequestMapping(value = “/”, method = RequestMethod.GET)
        public String home(Locale locale, Model model)
      “Welcome home! The client locale is {}.”, locale);
                Date date = new Date();
                DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
                String formattedDate = dateFormat.format(date);
                model.addAttribute(“serverTime”, formattedDate );
                return “home”;

Then lets look at the Test itself.

package us.kristjansson.springTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.springframework.test.web.servlet.MockMvc;
import org.junit.Before;
import org.junit.Test;

public class TxHomeController
         private static final Logger logger = LoggerFactory.getLogger(TxSample.class);
         private MockMvc mockMvc;

         public void setup() throws Exception
        “Setup CxHomeController” );
                 this.mockMvc = standaloneSetup(new CxHomeController()).build();

         public void TestController() throws Exception

        “testing CxHomeController” );
                 // The CxHomeController’s view is "home" without content


Maven Sonar plugin for your Java project

I needed to add Sonar reporting to a small Java project that uses Maven for the build. There is a Maven Sonar plugin available for this task here.

First add sonar properties file ( ) to your java project, put it in root.

# info
sonar.projectName=Sample java spring MVC project

# Comma-separated paths to directories with sources (required)

# Language

# Encoding of the source files

Make Maven aware of the Sonar location etc, add that to the Maven settings \maven\conf\settings.xml

Something like this

          <!– SERVER ON A REMOTE HOST –>

For Maven version 3+ add this to the POM file in the build section, else Maven version 2 ( below ).


For Maven 2.


Pass the sonar:sonar goal to the Maven script then kick off a build, the dependencies should be downloaded automatically. The report for your project displays on the Sonar server dashboard.

Databases are for …

Something I read recently reminded me of this.

At one point I was freelancing in New York city. This guy calls me up and wants to see if I can come over and take a look at this pogram “one of his coworkers” wrote. I’m like sure what’s going on with it, well I just want to make sure best practices were followed, we also seem to have some memory problems. Mind you this was early Java days and the program was written in Java. I show up in his office and he takes me to this laptop “the other guys laptop” I looked around but never saw the other guy. The IDE was open and the project loaded, lets take a look. I poked around some, the code seemed ok, the flow was fine.

But then I saw it, and I go hmmmm…. yea, what is it ? do you realize that all the data from the database is loaded into memory, right here. That’s what it’s doing, right ? Yes, that way it’s super fast…. but here is the thing when we have small amount of data the program works fine, but now I have to put it into production and we are using much more data. I tried my best holding a straight face and talking with him about databases and how they are great for querying for data. I left his office and promised to write up some suggestions about the code, how to make it better. I think he gave me a copy of the code to analyze. I wrote a list of 10 or so things and emailed it over to him. Needless to say the first suggestion was about not loading all the data from the database into memory !

A few days later I get an email from him which apperently he sent to everybody that had come over to evaluate the code, 5 or so consultants. He was asking for a short term fix, as the program needed to be demonstrated to management and he was still getting these memory errors when running with the bigger set of data. One of the consultants suggested a solution, increase the memory size for the JVM. It seems to have worked fine as I never got another email from him again, it’s great when you can find resonable fixes to your problems.

GoogleTv getting started

There were some GoogleTv guys ( Les Vogel and Paul Carff ) coming to town to hold a session on GoogleTv development. This event was facilitated by the local Android meetup group. Naturally I wanted to attend, I haven’t looked at GoogleTv before so why not jump on the oppertunity to get to know GoogleTv development a little. To my surprise I was handed a nice GoogleTv T-shirt and a Vizio GoogleTv device as I walked in the door, w00t !

Lets look at developing for the device, it’s recommended that you develop on Linux, as the Google Tv emulator uses KVM virtual machine to run in, most people use Eclipse as the IDE. Other than that setting up for coding for Google Tv is pretty much the same as coding for Android. First download and install the Android SDK, then download and install the Google Tv extension. Here is a little snag I ran into, when running the Android SDK Manager you will have to toggle the radio button to sort by repository rather than API level to find, “Google Inc. ( )” in the list. Once you see that you can find “Google Tv Addon” below, that’s the one you want, check the box and install the package. With that out of the way, next you need to setup a Android Virtual machine using the Android Virtual Device Manager. When you create your new GoogleTv AVD see if you can set the data partition to 1024 instead of 128, this will help with errors about the emulator not having enough space for installing your application. Otherwise you can also run it from command prompt, after you add the /tools and /platform-tools directories from the sdk to your path. Like this

$ emulator -avd googleTv -partition-size 1024

For some your keyboard might not work either in the Emulator, in order to activate that add Keyboard support in the harware properties of the AVD configuration and set it to “yes”. Now you should be good to go, you might have to start the Emulator from command prompt depending on your setup. Now in your Package Manger, you can look for KVM and install. You will also need computer that supports BIOS virtualization extensions, in order to check on that run

$ kvm-ok

you should see some message like “KVM acceleration can be used” if your good, otherwise you will get some kind of error.

That’s the Emulator at work

I wrote a sample app that plays different media over the internet, it turns out the emulator is not as capable of playing different formats as the regular Android emulator. Most of my streams played on the Android emulator but not on the GoogleTv emulator. It’s a good thing that they gave us GoogleTv devices when we attended the labs event at Google, now I can start coding and try it on the actual device which will work better than on the emulator. The Vizio GoogleTv is only $99 at the moment, if your going to develop for GoogleTv I would recommend getting the device to test on rather than using the emulator.

A lot of Android applications will work just fine on GoogleTv without any modifycation. The most important thing to keep in mind is that the GoogleTv display is Landscape only and the resolution is usually in the Tv format 1280 x 720 or 1920 x 1080. For example my Android BeerWidget runs fine on the GoogleTv without any modifycation.

HelloWorld deploy on Google Application Engine 1.6.4

I have seen a bunch of tutorials on how to deploy on the Google App Engine. Let me tell you my experience as well, no it does not take 5 minutes, it’s more like 15-20 minutes to get HelloWorld app going.

First you need application account, go to the location below, login with your Google account and create your own application

I use Eclipse ( Indigo ), so let’s look at how you install the Google Engine / SDK and plugin from there. The App Engine just got update to version 1.6.4, naturally that’s what we will use.

Help -> Install New Software

Use the Google Eclipse Indigo ( ver 3.7 ) feed

You can skip the Android stuff, just check the other 3 check boxes and start your downloads, you will have to Accept the license etc, it’s self explanatory.

After a few long minutes of downloads Eclipse will ask for a restart to install everything properly and you will be in business.

Create a new project,
File -> New -> Other -> Google -> Web Application Project

This will create a sample Google Application Engine project for you.

Then simply choose the blue (g) icon from the toolbar menu, and choose Deploy to App Engine… You will be asked for account info etc, and you will connect this deployment with the app you created in the beginning. Now watch the Console portion as the info about your deployment are displayed until you get success info. Then you can go to your application URL and see your new project in action. To find the URL you can goto My Applications in the Google app dashboard, it will have links on the right ( instances ) click on the link there and it will take you to your app.

Your URL will be something like

Its worth to mention that Google will host your app for free, as long as you don’t exceed certain resource limits. They actually have generous free limits, you can read more about the limits and what else you can buy in terms of resources here. You can actually host your own domain on the Google App Engine as long as it’s not a naked domain, That is you will be able to host but you will not be able to host That’s really because of security, you can find further reading about that here.

That’s all, happy coding !