7 reasons to upgrade to Java 7

Java 7 is finally here! Or to be absolutely correct, will be released July 28. In this post, I will point out 7 (wonder where I got that number from…) reasons to upgrade.

  1. Coins are also money
    Project Coin contains a couple of nice language changes that will make life as a programmer much easier. See the project page for details.
  2. Dynamic languages
    invokedynamic adds support for dynamically typed languages on the Java platform.
  3. New File System API
    File operations have always been pain in Java, but with this new API most of the issues are solved. Manipulating symbolic links for example.
  4. Concurrency
    The Fork/Join Framework provides a set of utilities you would benefit from when writing concurrent programs, giving the possibility for true parallelism on the Java platform.
  5. Modularization
    A refactoring enabling the Java SE platform to be downloaded as required by the VM as needed. 
  6. Enhancements
    A lot of enhancements regarding classloading, unicode, locale etc.
  7. It’s new
    And new things are always more motivating to work with than old, at least in the context of programming languages.

To be honest this is not very much for a major version of a programming language, especially since it has gone nearly five years since the previous version. But it shows that Java is still alive after the whole Oracle story.

Architect’s Java DAO Generator

Usually, when I come home from work, I am pretty tired of programming and rarely ever do any programmin during weekends. But this weekend was different. I have been coding pretty much at work lately, so it should not be because of abstinence from coding. Anyhow, I set down and contributed to a small open source project started by a former colleague of mine. It is called Architect’s Java DAO Generator, and you can find it on Sourceforge. In short it is a maven plugin that generates most of the boilerplate code you usually have to code by hand. It also abstracts the data access layer from your domain logic in a nice way. Version 1.5 is soon going to be available and is absolutely worth a look.

Candidate for Daily WTF

To cry…or laugh…that’s the question…

@Test
public void spaceFiller() {
   int l = 5;
   String result = MessageUtil.spaceFiller(5);
   if (result.length() == l) {
   } else {
      fail();
   }
}

When I came across the test code above I wondered who on Earth would write such a thing. The answer came shortly after when I had a look at the method it is testing…

/**
 * Space filler.
 * Creates a string filled with spaces of the wanted length
 *
 * @param length the length of the wanted string
 * @return the string
 */
public static String spaceFiller(int length){
   String s = "";
   for(int i=0; i < length; i++)
      s = s.concat(" ");
   return s;
}

The good thing is that they actually wrote a nice and clear JavaDoc explaining their own stupidity…

Are Unit Tests Necessary?

At work today I came across a project that had not written a single unit test for their new code and wondered what the hell they were doing. Nothing unusual except that apart from the usual excuses about time constraint etc., they had the guts to challenge the value of unit tests. I did not believe what I heard and for a moment wondered if I had gone through some kind of time capsule when I was visiting the pyramids in Cairo last week and come back in a time before unit testing was invented. But a quick glance at the date on my watch ensured me that it was still 2010.

The only comforting thing about this is that as long as there are projects like this, there will be plenty of work for software consultants to clean up the mess. Just a pity that the first couple of weeks will be spent writing unit tests to be able to start refactoring the code. Good thing that writing unit tests are pretty fun and addicting, or to quote one of Kent Beck’s tweet earlier today “… tests are like potato chips”.

A Special Year

The first time I attended JavaOne was 1999 and I have only missed it once since then. Sadly, this year will be the second time I am not present there. I have become kind of used to the week in San Francisco every year. It is the perfect way to start the summer with a visit to that beautiful city. Since it is in September this year, it would probably been the perfect way to end the summer (…we have short summers here in Scandinavia…).

I will for sure miss the massive input and inspiration this conference gives me and enables me to keep up-to-date on everything that is happening in the Java Community. This year’s conference is also special since it is the first time Oracle is hosting the show. It feels like a good idea to co-host it with Oracle Develop and I hope it will be a success to be continued. Next year, I will definitely be attending, one way or the other…!

Android Emulator Workaround

Here is a small tip if you are having trouble running the Android Emulator for code that calls native methods and getting an error message similar to this:

JNI WARNING: method declared to return 'Ljava/nio/ByteBuffer;' returned 'Ljava/nio/ReadWriteDirectByteBuffer;'
... ;.nativeAsBuffer ('Ljava/nio/ByteBuffer;' not found)

The simple workaround is to add the following to the onCreate method of your main activity:

ByteBuffer dummy = ByteBuffer.allocate(0); dummy = null;

This way the classloader is “forced” to load ByteBuffer. This is not needed when running on a device, only the emulator as it seems.

NetBeans and Android Tip

Developing Android applications using NetBeans is usually as easy as stealing candy from a baby. But the last couple of days I have been struggling with an application that uses a couple of external libraries. The other developers (using Eclipse) have a couple of scripts that they run to get the .so files included in the .apk file. When I tried running the same scrips on the .apk generated from NetBeans, the application failed to start in the emulator. I nearly switched to Eclipse (god forbid), but then I saw the light again. As it turns out, NetBeans does not include the java api jar-files in the external libs in the dex-file by default which resulted in a ClassNotFoundException.

The solution is as simple as you would expect when you have used NetBeans for a while. Add the following to the build.xml file in the project root (replace the dummy values for the signjar target):

<target name="-pre-jar">
   <copy todir="${build.classes.dir}">
      <fileset dir="${external.libs.dir}">
         <include name="*.jar"/>
      </fileset>
   </copy>
</target>
<target name="-post-jar">
   <zip update="true" destfile="${dist.apk}">
      <zipfileset dir="${external.libs.dir}" includes="*.so" prefix="lib/armeabi"/>
   </zip>
   <zip destfile="tmp.apk">
      <zipfileset src="${dist.apk}">
         <exclude name="META-INF/*.*" />
      </zipfileset>
   </zip>
   <move file="tmp.apk" tofile="${dist.apk}" />
   <signjar jar="${dist.apk}" alias="alias" storepass="secret" keypass="secret2" keystore="my_keystore"/>
</target>

You also need to add external.libs.dir=<your lib folder> to you <project root>/nbproject/project.properties file.

Now you can install the resulting .apk file using adb install or by running/debugging directly from NetBeans. Remember to follow the tip for asset-files in a previous post if you have that kind of resources.

Android Emulator and NetBeans

Whenever you ask or search for help regarding Android development, you end up with some fix related to the Eclipse ADT plugin or the Android SDK tools. My intention is to fix that by repeating parts of a great tip I found at Tim Perry’s blog. It is about how to get hold of the resources placed under the /assets folder in you Android project while running your applictaion in the Emulator from NetBeans. If your application tries to access resources from the AssetManager you will get a FileNotFoundException. The reason for this is that the assets are not packaged with the .apk like it is if you package and deploy it using the SDK tools.

The solution is:

  1. Go into nbproject/project.properties and add ‘assets.available=true

Voila! You will now be able to run, debug and step through your code as you would expect.

Google App Engine

It is amazing what a month in South Africa does to you. Things like Twitter, Facebook and blogging becomes pretty distant… But now I have been home for a while, Sun+Oracle has been approved by EU, I have finished (almost) my kitchen renovation and celebrated yet another birthday, so it is time to get started again!

After a discussion with a colleague at a coffee break this morning, I decided to try out Google App Engine. The getting started guide is a great place to start. It gives you a great walk-through setting up the development environment and creating a sample application. Since I am no big fan of Eclipse, I installed the Google App Engine Plugin for NetBeans. After resolving a small issue regarding path settings (see solution here), it was up and running perfectly.

Next steps will be to figure out what changes that has to be made to my existing applications to be able to deploy them on app engine. Probably the server side of YouOweMe will be the first candidate. Or maybe the KanbanFX server. I haven’t decided yet…

Company Internal Twitter – good or evil?

This week Yammer was introduced to the entire company. Simply put, Yammer is a company internal twitter where coworkers can connect and share information by posting messages.

At first, I was skeptical to the whole idea thinking it would generate an overload of noise for a couple of weeks until dying slowly like most initiatives to share knowledge within companies. But after having thought about it, I really hope that will not happen. It is actually a brilliant way of building a knowledge base within the company. Everyone who has tried to establish some form of knowledge exchange know how hard it is to get people to contribute.

But by “hiding” it behind some familiar technology like twitter, people actually contribute without knowing it. If you think about it, only the things people are interested in will be posted and discussed in such a forum. People will only put energy in discussions they have strong feelings for. The things most people have feelings for are probably pretty relevant things for your organization. And it is by default fully searchable with the newest items most visible.

So with my limited experience of yammer (have used it two days), I will conclude that it is actually a good thing. Now we just have to hope that it does not die a silent death when it is not that new-and-cool anymore…