Wednesday, 11 August 2010

XPath Expression Testbed

I've just been writing some code to parse an XML file using XPath and came across this very useful web page at

XPath Expression Testbed

It allows you to upload a sample of the XML file you are trying to parse and experiment with XPath expressions. Much easier than trying things in Java code, recompiling and running. There is also a good XPath tutorial linked on the testbed page.

Very handy!

Tuesday, 3 August 2010

Java and Gnome

I don't often write GUI applications but I have been experimenting over the last few days with the Java-Gnome User Interface Library for writing Gnome-based applications in Java.

The disadvantage of this of course is that you forego the "write-once-run-anywhere" ethos behind Java; the finished application will only run on a system that uses Gnome as its window manager. The standard, cross-platform way of doing things is to use Swing but the "native" look & feel widgets are only imitations of the real thing and, Windows aside, can look like poor imitations.

Compare these screenshots for example, of a colour chooser dialog in Swing on a Gnome desktop (Ubuntu 10.04) and a native Gnome application:

I suppose the best compromise would be to use the Standard Widget Toolkit (SWT), as used by the Eclipse platform, which is a standard interface underpinned by native windowing system widgets.

The Java Gnome User Interface Library

Subject to the above caveats, my first impressions of the Java-Gnome UI Library is very good. I don't think all of GTK is covered as yet, but there is certainly enough there to write perfectly serviceable applications for Gnome desktops. As an exercise, I managed to knock together a perfectly serviceable text editor in a couple of days, complete with syntax highlighting, cut/copy/paste, undo/redo, open/save dialogs and shortcut keys.

I found the code quite simple to write and was up and running quite quickly. Creating things like menus and toolbars is made very easy with the concept of actions, and standard framework actions (stock actions) keep things consistent. Handling events is straightforward when you have written a couple of event handlers. The trickiest part was getting round a few subtleties of the undo/redo mechanism, which kept crashing the application because I didn't understand it properly. But rock solid now.

As with any GUI framework, code can get into a mess very quickly, so some organisation is essential. The approach I used on the editor exercise was to make the main application window a subclass of the Window class, set it up as a singleton and have it create the other components it needed in its constructor. Any code in the components can then look up the singleton instance and access methods and data in the other components in a controlled way.

There is an excellent tutorial available to get started and the API documentation is very good.

Well worth a try if you want to write some applications for the Gnome desktop in Java.

Thursday, 29 July 2010

Maven Web Projects and Eclipse

Apart from dependency management, perhaps the biggest benefit I get from using Maven is being able to distribute sample code to people from my training courses and not worry too much about which IDE they are using. Because Maven separates the build process from the IDE, it makes it reasonably straightforward to open Maven projects created in other IDEs.

NetBeans is my IDE of choice and Maven support is bundled with the IDE from 6.7 onwards. There is no setup to do in this case (apart from perhaps pointing Netbeans at a local installation of Maven rather than the built-in version). Simply open the project and Netbeans will identify it as a Maven project by the presence of a pom.xml file.

With Eclipse, Maven support is not included in the base IDE. Other offerings, such as the excellent SpringSource Tool Suite, do include it. Mainly For the benefit of people on whom I inflict my demo projects and examples, here is a quick run-down on getting started with Maven and Eclipse.

Setting up Eclipse for Maven Projects

I'm assuming that you have installed your base plugins for whatever type of Java development you intend to do with Maven: the Web Tools Project (WTP) plugins if you are going to do some web development for example.

To add the required plugins to Eclipse, you need to go to and add the following update sites:


The names don't matter of course. "M2Eclipse Plugin" and "M2Eclipse Plugin Extras" are as good as any others.

For the M2Eclipse Plugin, there is only one component to select. Make sure you accept any licence agreements and certificates during the installation. This will give you basic Maven support for building Java (non-Web) projects. It's a common mistake to either forget or not be aware of the "extras" plugin but if you try and open a web project you'll end up with a complete mess in the Project Explorer because Eclipse doesn't understand the Maven web project layout.

For the M2Eclipse Plugin Extras, there are more choices and you probably don't need them all. In fact the only ones I tend to use are the Maven Integration for WTP and Maven SCM Integration. The latter is only required if you want to check out Maven projects from a version control system like Subversion (in which case you'll need the relevant Eclipse plugins to support that directly, as well as the Maven SCM Integration).

And that should be it. Open up the Maven project and Eclipse should show that it has recognised it with an "M" in the top left corder of the project icon in the Project Explorer.

In Eclipse, Maven goals are invoked using the Run menu or toolbar icon. To clean the project for example, right click on the project node, select "Run As", then "Maven Clean". To build the project and deploy the resulting JAR or WAR file to your local Maven repository, select "Run As", "Maven Install". To run a web application on an Eclipse-controlled application server, just choose "Run As", "Run on server" as usual.

Thursday, 3 June 2010

Continuous Integration

I've been spending some time this week looking into setting up a continuous integration server for some of my training and personal projects. Although these projects are not developed by a team, which would be where continuous integration would really become useful, I was finding that I was spending quite a bit of time just keeping projects up and running following changes to upstream dependencies.

There are plenty of choices out there for continuous integration and related tools. My setup is now based on:

Certainly the result is working well. No more scuttling around checking that example software builds before I go off to teach a course. I can be pretty certain now that I just need to check the latest versions of my projects out of Subversion and that I haven't broken anything.

Apart from my desktop machine, which is my main development box, and my laptop, which I take on courses, I'm running the core tools on a very modest server: dual Celeron 500MHz with only 768M memory. I've had to do some performance tuning with the JVMs to reduce the overall memory footprint (my poor server was working hard yesterday and the swap partition was getting hammered), especially as the server also runs Apache, Tomcat and acts as a RAID 5 fileserver.

I'll be making some articles over the coming weeks on experiences I've had with this setup and the benefits I'm seeing from these tools.

Saturday, 29 May 2010

Oracle Sun JDK vs OpenJDK

These days Ubuntu comes with Open JDK installed as standard. Many Java programmers replace this immediately with the Oracle Sun JDK.

So what's the difference? Well, the OpenJDK is a 100% open-source implementation of the Java language specification. The Oracle Sun JDK is largely open-source but still contains some precompiled binaries that Sun didn't have copyright to release under an open-source license.

You can read more here:

Oracle - Free and Open Source Java

I've always tended to replace the OpenJDK with Sun Java, but since installing Ubuntu 10.04 Lucid Lynx, I've been seeing how I get on with the OpenJDK. To date, developing straightforward Java, JSP and servlets with Spring, Struts2, Hibernate and Maven deployed on Tomcat, has worked perfectly well. I've also been using the IcedTea plugin in Firefox with no issues.

But yesterday, I hit my first snag. If you read my previous post you'll know that I've been looking into the new annotations in the Servlet 3.0 specification: @WebServlet, @WebFilter and @WebListener. To get examples to run I had to install Glassfish v3. All was well until I tried to run the Admin Console and I got this error:

java.lang.UnsupportedOperationException: Cannot create XMLStreamReader or XMLEventReader from a
        at org.jvnet.hk2.config.ConfigParser.parse(
        at org.jvnet.hk2.config.ConfigParser.parse(
        at org.jvnet.hk2.config.ConfigParser.parse(
        at org.glassfish.admingui.plugin.ConsolePluginService.init(
        at org.glassfish.admingui.plugin.ConsolePluginService.getIntegrationPoints(
        at org.glassfish.admingui.common.handlers.PluginHandlers.getIntegrationPoints(
        at org.glassfish.admingui.handlers.ThemeHandlers.getThemeFromIntegrationPoints(
        ... 47 more

That didn't worry me too much at the time, but I was also getting some odd behaviour with Java Server Faces (JSF) applications that I hadn't seen on Tomcat. Most of the time I'm able to switch freely between one and the other with web applications.

So, based on a forum post about the problems with the Glassfish admin console, I installed all the Sun Java 6 components. My problems with the admin console disappeared and my JSF application started behaving normally again. So, on that basis, there seem to be glitches with JSF using the current version of the OpenJDK. The Glassfish admin console is based on JSF.

If you need to replace your OpenJDK installation on Ubuntu 10.04, these are the steps:

  1. Enable the partner repositories: System -> Administration -> Software Sources and check the box for lucid partner in the Other Sources tab. You might also want the sources checked too.
  2. Install the Sun Java6 packages: sun-java6-jdk, sun-java6-jre, sun-java6-source, sun-java6-fonts, sun-java6-bin and possibly sun-java6-plugin.

You'll then need to switch your default Java to the Oracle Sun version:

$ sudo update-java-alternatives -v -s java-6-sun

Do also check that you haven't hard-coded OpenJDK into any JAVA_HOME variables or application settings.