RSS Feed Subscribe to RSS Feed


Ant build file to redeploy to Tomcat

Here is an example of an ant file that redeploys a web project to tomat. The neat thing is that as well as creating the war file, it also stops and restarts tomcat, all from ant.
I have found myself rewriting variations of this on a couple of projects recently, so I thought it was worth posting…
(Note it doesn’t include a compile target as I am doing it from my IDE)






And here is an example of the associated file

	tomcat.home=C:/Program Files/Apache/tomcat

Tags: ,

Details of the new Sun Java Programmer Plus Certification

I posted previously about the new Sun certification exam announced at Java One, but there weren’t many details at the time. However, I found this blog post which points to Sun’s web site where they announce and give details of the new Sun Java Programmer Plus Certification. Apparently is will launch on July 22. No word on when the old SCJP will be retired, but I suspect it will be soon. Sun say that they will “provide a public notification of at least 90 days in advance of any exam retirement”.

Tags: , ,

Eclipse 3.5 (Galileo) released

Eclipse 3.5 (Galileo) was released last week. I’ve been using the Release Candidates for a while, but the official release seems like a good time to post about some of the new features available that have been of most use to me…

  • An Open Implementation hyperlink has been added for overridable methods (you hold down the Ctrl key click on the overridable method to the list of implementing classes). I had been using the Implementors plugin to provide similar functionality, but it’s nice to finally have it as part of the standard release.
  • The Java compare editor now supports new text editing features like move/copy/delete line and formatting, and updates its structure while you are typing inside the editor
  • Introduction of a toString() generator
  • Although the ability to generate hashCode and equals methods was already present, the compiler can now detect whether a class overrides one but not the other (hence breaking this golden rule from Effective Java)
  • The compiler offers several new diagnostics, including finding dead code and warning when comparing identical values (e.g. would flag if var==var), along with suggested fixes.
  • The Memory Analyzer tool now comes built in
  • The JUnit4 version shipped with Eclipse has been updated to 4.5.

As well as some little but handy enhancements like:

  • Double clicking on the beginning or end marker of a comment now selects the whole comment.

  • In the headers of Javadoc hovers and the Javadoc view, references to other types and members are now links.

See also:

Eclipse 3.5 – New and noteworthy

Tags: ,

Eclipse not hitting breakpoints

While trying to debug some code recently in Eclipse, I have been running into an issue where the code has not always been stopping at breakpoints. It has been happening inconsistently, with the breakpoints sometimes getting hit, sometimes not, and it has been driving me crazy!

I finally tracked down the problem after spotting this blog post, which points to a known problem with the version of the JVM (1.6.0_14) I have been using. Downgrading to this previous version has solved it. Thankfully.

Tags: ,

Spring and EJB 3 Integration

I attended another excellent SF JUG meeting earlier this month. It was a double billing with Talip Ozturk talking about Hazelcast, an opensource clustering and data distribution platform, and Reza Rehman speaking about EJB3 and Spring Integration.

Reza is the author of EJB3 in Action and an accomplished speaker whom I had the chance to meet at The Server Side Symposium earlier this year, so I was particularly interested in his talk.

Reza gave some background on EJBs and how they have been completely reinvented as part of the latest (EJB3) release, including using 100% annotations (no xml) and making heavy use intelligent defaulting. He also talked about how Spring became popular as an alternative to the heavyweigth approach of the older EJB releases and has thrived through its focus on integration with standards like JPA, JMS, JDBC and JAX-WS.

He then went on to his main point, which was that EJB3 and Spring can now be viewed as complimentary, rather than competing, technologies and he went on to back this up by discussing the integration strategies that can be used, including

  • Embedding Spring inside a Java EE app server
  • Embedding an EJB3 embeddable container withing Tomcat with Spring
  • Enabling Spring @Autowired annotation in EJBs via Interceptors
  • Using EJB3 natively inside Spring using Spring Pitchfork

Reza finished by reiterating the benefits of using Spring and EJBs together to increase ease of coding and vendor neutrality.

Overall, I thought it was an insightful presentation from Reza. I had studied EJBs back in the v2 release as part of the my SCEA certification, and have used Spring in several projects, but this was the first time I had a chance to hear how they can be used together.

I have made Reza’s presentation notes available here, as well as his demo source code available here. You can also follow him on his web site and blog at:

Tags: , ,

TheServerSide Java Symposium – Day 2

Day 2 at TSS Java Symposium.

The highlights of the second day at TSSJS2009 were a couple of interesting talks from Rod Johnson (Mr Spring) and a talk on Groovy from Scott Davis (

I have included links to some of my (limited) notes below, which includes links to the actual presentation slides (PDFs) where available.

Keynote: How Spring Fits into the Java Landscape – Rod Johnson

Spring for the advanced developer – Rod Johnson

The Amazing Grrovy Weight Loss Plan – Scott Davis

Tags: , , ,

TheServerSide Java Symposium – Day 1

Today I’m at the first day of the TheServerSide Java Symposium in Las Vegas. There are about 400 attendees and a dozen or so companies exhibiting. I will try to post some notes about the talks as the day goes on…

Tags: , , , ,

Exceptions versus Return Values

In a previous post here, I blogged about checked exceptions and how I felt they are a useful tool in your Java toolbox. It is generally accepted that errors, or unrecoverable events, should be dealt with using unchecked exceptions and that recoverable problems, or contingencies, should be dealt with using checked exceptions.

However, there is another option for dealing with recoverable problems, i.e. using return values.

Read full article & Comments


Checked Exceptions

The use of checked exceptions in Java is still widely debated. This post contains thoughts on their use by Java experts such as James Gosling, Joshua Bloch (Effective Java) and Bruce Eckel (Thinking in Java), before countering some of the arguments against their use, such as how I don’t believe they break encapsulation, and that their use shouldn’t be limited to truly exceptional conditions. It finishes by looking at when alternatives to checked exceptions should be used. The overall slant of the article is that there are many reasons why Exceptions, including checked Exceptions, should still be a part of your Java arsenal.

Read full article


JavaFX Plugin for Eclipse – too buggy?

After attending a meetup on JavaFX last month, I have been trying to get more familiar with the technology. I know that NetBeans is the ‘recommended‘ IDE for JavaFX, but I am an Eclipse devotee and thought that surely anything NetBeans can do Eclipse can do. Not in this case it seems.

Some simple JavaFX script just don’t seem to work with the Eclipse plugin. For example, the following snippet:

Stage {
    title: "Nodes"
    scene: Scene {
        fill: Color.LIGHTBLUE
        width: 220
        height: 170

Causes the following error:

incompatible types found: integer required: com.sun.javafx.runtime.location.FloatVariable

The same snippet compiled and ran straight off when I tried it before in NetBeans.

The only posting on it I could find is here. I took the advice and downgraded to JavaFX 1.0, which seems to have done the trick for now. If anyone out there has any suggestions on better solutions/fixes, I would love to hear them.

This isn’t the first time I’ve spotted some better GUI related functionality in NetBeans than in Eclipse (e.g. I briefly mentioned the NetBeans GUI Builder tool a while ago). There is no chance of me stopping using Eclipse completely, but maybe I will start using both…

Tags: , , , ,

Running Hibernate unit tests with Maven

I recently converted a project I have been working on to use Maven. After setting up all the dependencies in the pom, I got everything compiling fine but ran into problems getting the unit tests to pick up the hibernate config (hibernate.cfg.xml) and hibernate mapping (*.hbm.xml) files. With hindsight, it is straightforward, but it took me a while to figure it out so I thought I’d post the solution here.

Initially, I had my hibernate.cfg.xml file in the following directory


And when I first tried to run my hibernate unit tests (mvn test), I got this error:

SessionFactory creation /hibernate.cfg.xml not found

Some checking of the maven docs and some forums revealed the following possible solutions:
1) Copy the hibernate.cfg.xml file to


This works, however I think it is a hack as the target folder is generated rather than being somewhere you should actually manually create files.
2) The next solution I found suggested moving the hibernate.cfg.xml to


This works well as the contents of this folder are copied to the base level of the my-app/target/classes folder so it is basically a ‘more correct’ solution than the first.
3) The next and I think best solution was to create a new, duplicate hibernate.cfg.xml inside


This allows a different hibernate.cfg.xml file to be used for testing and, for example, facilitates connecting to a different database (such as hsqldb) for testing while continuing to use your regular database (such as MySQL) for the app itself.

A couple of points to note:

1) The hibernate mapping files (i.e. the *.hbm.xml files) should also be in the resources folder (in whatever directory structure you choose) to ensure that these too are accessible by the unit tests.

2) I am not (yet) using Spring with the hibernate components, which would likely have changed the above setup.


Tags: , ,

JavaFX Meetup

I attended another one of the excellent San Francisco JUG meetings tonight. This time the topic was Java FX and WidgetFX. Java FX is Sun’s new platform for delivering Rich Internet Applications (RIAs), competing in the same space as Adobe’s Flex and Microsoft’s Silverlight.

It is a fairly new technology with the first 1.0 release of the Desktop platform just happening last December. The Sun web site seems to show the Mobile platform as being available now too. Wen Huang from Sun talked about the planned 1.1 release, but was strangely coy when asked about the ‘TV platform’ that is also planned. He mentioned a tie in with Sony and Bluray so I guess we an expect more details on that in the near future.

Wen explained that JavaFX is built on top of Java, in that it runs on top of the Java Plugin and hence it can access to all the usual Java libraries.

Doris Chen, a Staff engineer and technology evangelist at Sun, then took over the presentation and got a little more technical. She quickly walked us through a couple of demos involving animation and videos in the form of puzzle games. It did look pretty cool. She also demonstrated how Java FX apps could be dragged out of the browser and continue to run independently, even when the browser was then closed. This is possible due to each widget running in its own Java VM, independent of the browser. I did notice those annoying Certificate security warnings popping up during the demo startup though which made me wonder… Would using JavaFX be as seamless for the end user as Flash, or would there be security, download, startup and plugin compatibility issues that applet usage sometimes involved?

Next up, Stephen Chin took over. Stephen is the co-author of a soon to be published book called Pro JavaFX book. He is also closely involved with two open source JavaFX libraries, JFXtras and WidgetFX. He walked us through a couple of code examples. The things that jumped out at me most about JavaFX were

  • the declarative construct used for creating objects, which I am not too familiar with (similar to the Scala approach maybe?)
  • extensive use of a ‘bind’ operator which seems to ensure that variable value changes get reflected in the UI
  • JavaFX doesn’t seem to throw many exceptions, instead it tries to handle them as gracefully as possible
  • It is a Functional language: Functions are first class entities that can be assigned to variables, passed in and returned from functions

The class finished with Stephen walking us through some hands on examples, using NetBeans (Update: see my comment at the end of this article on the JavaFX Eclipse plugin) and both WidgetFX and JFXtras.

Overall, I thought the presentations were very good and provided a great overview, and some of the details, of JavaFX. For me personally though, none of the projects I am currently working on have a requirement for ‘rich’, multimedia type content, but it would be nice to create a few more mundane desktop type JavaFX GUIs to see how it fares compared to the Swing GUIs I have created in the past.

I do have some concerns about how to integrate JavaFX GUIs with the web tier and backend. Doris Chen tonight mentioned JSON integration, but there were less details on how JavaFX would integrate with model-2 MVC libraries like Spring MVC or Struts. I will definitely watch the JavaFX space closely to see how widely accepted it becomes, but I am also interested in learning more about other Java GUI technologies that I haven’t had too much exposure to yet, such as JSF and Facelets. Perhaps in another JUG meetup soon…

Tags: , , ,

Persisting Money class with Hibernate

Further to my previous post about representing monetary amounts in Java, I have been using the TimeAndMoney library.

I ran into some problems persisting my classes that use Money via Hibernate. I should point out that these are not problems with the Money class or the timeandmoney library, rather than with my hibernate setup, but I thought I would post my approach here for future reference.

I started by incorrectly trying to map my Money field (in my case, called ‘openingBalance) as a property, viz:

<property name=”openingBalance” type=””>

Which resulted in the following error:

SEVERE: Data truncation: Data too long for column ‘openingBalance’ at row 1
Exception in thread “main” org.hibernate.exception.GenericJDBCException: could not insert…

Then I tried mapping it as a component, viz

<component name=”openingBalance” class=””>

<property name=”amount”/>

<property name=”currency”/>


But this resulted in the following error:

Initial SessionFactory creation Could not find a setter for property amount in class
Exception in thread “main” java.lang.ExceptionInInitializerError

This is because the Money class has no setAmount or setCurrency methods. The solution is to specify field access (as opposed to accessor method access), as in

<component name=”openingBalance” class=””>

<property access=”field” name=”amount”/>

<property access=”field” name=”currency”/>


Note that if you check the Money javadocs, it (correctly) says that the setters are not provided because they would break encapsulation They do however (and I quote, “begrudgingly”) provide alternative setters such as setForPersistentMapping_Amount, but I am not sure how to get Hibernate to use these, and in this case, I don’t think it is necessary or beneficial anyway.

Tags: , ,

Representing Money in Java

I’ve been writing some code recently that required dealing with monetary values but, as hard as it is to believe, there still doesn’t seem to be a great solution for money in Java. I’ve known for a while that float and double should never be used for representing money, or indeed anywhere exact values are required (e.g. 1 – 0.9 may give you something like 0.09999999999999998). Using long or int gives exact answers but requires keeping track of the decimal point yourself, which is a pain. A common approach is to use BigDecimal, but that is not without its limitations either e.g. it is certainly clunkier to use than primitives, and slower, and the rules for rounding and presentation seem a little complex.

Another solution is to use timeandmoney. It is a little worrying that it is still pre-alpha (v0.5.1 at time of writing), but I am going to go with it in the absence of any better contenders.

Note that some of this is covered in Josh Bloch’s definitive book, Effective Java, that I posted about before, and there are some discussions about the issues here.

Tags: ,

The Server Side Symposium

Have just signed up for this year’s TSS Symposium. Couldn’t make last year’s in Prague, so looking forward to this one a lot. And it’s in Vegas so will have to brush up on my Blackjack and Poker skills again before then 🙂

Tags: ,