Sunday, October 27, 2013

Write Once Run Anywhere .... if you can

Let's see. A few weeks back my hard-drive crashed and I needed to replace it. Luckily, I had saved my data previously, among which a Java-based application with 3 tiers: JSF, jQuery, servlets, and an open-source database. I wanted thus to resurrect this application. "Piece of cake" I told to myself, Java is after all WORA (Write Once Run Anywhere).
So this is what I've done:

  1. downloaded and installed Eclipse Juno (which didn't start on double-clicking the exe, but rather using this command line: C:\eclipse\eclipse.exe -vm "C:\Program Files\Java\jre7\bin")

  2. I want to run this application on Apache Tomcat and Glassfish application servers, so after downloading and installing both, I needed to download server adapters, as shown in the following figure (see "Download additional server adapters" in the figure below)

Note that I tried first with Glassfish 4.0.

3. I created a Dynamic Web Project in Eclipse and imported the sources of my project (saved from the previous hard-drive)

4. Then, as I was building the project, I noticed missing required jar files, so I started looking for them (either jarfinder.com or findjar.com) and adding them to \WEB-INF\lib

At this point in time I thought everything should work fine. 
But first I encountered a strange run-time exception, as shown below:

5. After a bit of searching online, it turned out I needed to configure Project Facets to use Dynamic Web Project 3.0, as shown below:

Note: it's very important to select the right Project Facets, because that decides what jars will be used. Fro example, if you don't choose the JSF facet, then you need to add the JSF jars yourself; sometimes there's a good reason to do just that, e.g. the default provided jars won't do the job, so you want to use other jars.

6. As you can see, Eclipse complained about the Java version, so I needed to use the Java 7 compiler, as shown in the following figures:


7. After all this hassle, my application worked, but only with Glassfish 3.1; on 4.0 it throws an NPE; on Tomcat, no way to make it work, even though the same application previously worked ONLY on Tomcat and not on Glassfish ....

By the way, in the past I also had an issue with an application working fine on the former Oracle OAS and not working at all on Tomcat. It turned out that replacing the standard Oracle JSF libs with the ones from Mojarra helped. But you never know ....

I need to investigate this further, but now I'm happy at least I made it work on Glassfish 3.1.

Greetings from the jungle,
    Sorin


P.S. A great article about JSF 2.0 with Glassfish and Eclipse is here: http://balusc.blogspot.nl/2011/01/jsf-20-tutorial-with-eclipse-and.html.