Specifying a different config file for Log4J

Generally, adding a log4j.properties file to the classpath is all that needed to bootstrap your Log4J runtime. In certain cases though you might want to override this behaviour.
I needed to do this recently and it took me a while to find out how to do it in the Log4J docs, so I though I’d make [...]

Getting a “Netscape security model is no longer supported” error when using IntelliJ

I’ve been working on a JMS application that uses encryption with JNDI authentication and have encountered the following error message when running it:

Netscape security model is no longer supported. Please migrate to the Java 2 security model instead.

It turns out that this is a problem with the JDK’s plugin.jar file and can be fixed quite [...]

Open Session in View Pattern for Spring and JPA

The Open Session in View Pattern is well publicised in Hibernate circles as the best practice approach to presenting data in the Web tier of an application. In this article I’ll demonstrate how simple it is to configure a Spring application to do the same thing with the Java Persistence API (JPA).

Using Mockito to Unit Test Java Applications

If you’ve spent any time writing unit tests then you’ll know that it’s not always straight-forward. Certain things are inherently hard to test. In this post I’ll show you the basic principles of creating mock objects with a little help from the Mockito mocking tool.
One common problem faced when unit testing is how to test [...]

Using Assertions in Java

I’ve been working on a project this week and have been delighted to see that the original team of programmers who developed it have made the effort to use assertions in their code.
The assert keyword was introduced in Java 1.4 and has since been a sadly under-used language feature. Assertions offer developers a neat [...]

Counting the Days Between Two Java Dates using a Gregorian Calendar

Today I needed to figure out a way to count the number of days between two dates in Java. There are no API methods that will do this for you so a little work is required to achieve this. This post illustrates the solution I came up with.

The Details of Member Level Inner Class Syntax

Inner classes offer are a powerful extension to Java’s object model. However, the syntax for some of the more advanced features of member inner classes is quite tricky to get to grips with. In this article I’ll explain the full details of this syntax.

An Explanation of ‘this’ in Java

As every Java programmer should know, code within any non-static method can access the current object via a special reference called ‘this’. Just how this works may not be immediately obvious so I’ve taken a moment to explain it here.

Programming with Inner Classes in Java

Inner classes are an advanced programming feature in Java. They add another dimension to the concept of Object Oriented programming by allowing objects to not only reside alongside one another, but also within one another too. In this post I’ll explain how inner classes work with the help of an example that illustrates their most common use: Swing GUI programming.

How to strip accents from Strings using Java 6

I recently needed a function for stripping characters from a String in Java to make searching for words in a text simpler.
Luckily I was using Java 6 which has a new class in the java.text package that provides a really simple solution for doing this: the Normalizer.
The Normalizer class is capable of performing decomposition of [...]