Java Packages

Java, much like any other open source system, suffers from the paradox of choice - too many choices confuse people and makes them like the overall product or system less. There are numerous Java packages out there that do a number of things and it sometimes becomes very difficult for a developer starting on a project to figure out which combination of libraries are needed to meet the requirements.

Here's a list of interesting and useful projects that I have come across so far, ordered by the specific business need. While there are a number of comparison sites for libraries within a particular group, this is more of a subjective view of the libraries that I prefer.

Business Need Package
Validate US postal information (City, State, Zip) JPostal
Unit testing module JUnit
System or Integration testing with a Wiki-like interface - very helpful for transactional systems Fitnesse
Application framework without the overhead of J2EE Spring
Logging mechanism Log4J
Process Microsoft Office Documents Apache POI
PDF creator / generator iText
Reporting Engine Jasper Reports
Database mapping Hibernate
Search Engine Lucene


Creating a Singleton in Java

Here's a simple way to create a Singleton class

Read more: Creating a Singleton in Java

Multithreading in Java

Here's a simple framework that I wrote for multithreading an application.

Read more: Multithreading in Java

Adding Shutdown Hooks in Java

We all know that we can do cleanup for a try-catch block using the finally clause. How would would you do the same thing for an application? For example, if you are using some database resources, how can you guarantee that your application will free resources when it shuts down? Will your approach work if the user presses Ctrl + C? Or for that matter anything short of a power-off?

A relatively unknown feature in Java is a JVM shutdown hook. A shutdown hook is literally like a finally clause. It is called when the program is about to exit - either normally or abnormally. Setting it up is extremely simple. Here's how you do it.

First create a class that extends the Thread class (not sure why not the Runnable interface. Maybe they hadn't explored Programming To Interfaces then!) and put the code you want to get executed when the program is about to exit.

Read more: Adding Shutdown Hooks in Java