18 Nov 2009
Classloading in Rome
As a side-project at work, I’ve been working on an ant task to generate an atom feed of something. The something’s not really important.
Rather than re-invent the wheel, I did a quick search on google for a Java library to take care of the feed processing for me. Sure enough, Rome seemed to fit the bill.
Merrily testing away in my Eclipse workbench, I quickly wrote the code to generate my atom feed. All was good with the world. The planets may or may not have aligned. And then I came to run it through ant.
Unfortunately, that’s where the problems started…
BUILD FAILED C:\testAnt.xml:6: java.lang.ExceptionInInitializerError at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:115) at org.apache.tools.ant.Task.perform(Task.java:348) ... Caused by: java.lang.ExceptionInInitializerError at java.lang.J9VMInternals.initialize(J9VMInternals.java:222) at com.sun.syndication.io.SyndFeedInput.build(SyndFeedInput.java:110) ... Caused by: java.lang.NullPointerException at java.util.Properties.load(Properties.java:267) at com.sun.syndication.io.impl.PropertiesLoader.<init>(PropertiesLoader.java:74) at com.sun.syndication.io.impl.PropertiesLoader.getPropertiesLoader(PropertiesLoader.java:46) at com.sun.syndication.io.impl.PluginManager.<init>(PluginManager.java:54) at com.sun.syndication.io.impl.PluginManager.<init>(PluginManager.java:46) at com.sun.syndication.feed.synd.impl.Converters.<init>(Converters.java:40) at com.sun.syndication.feed.synd.SyndFeedImpl.<clinit>(SyndFeedImpl.java:59)
Bugger.
A quick google only turns up 2 useful hits: here and here. Both links seem to suggest that Rome can run into problems with the way it loads its classes. After much hacking around, I found that a (somewhat hacky) way to solve this was by over-riding the Thread’s ClassLoader in my main class; e.g.
public class Test extends Task{
public void execute(){
ClassLoader cl = Test.class.getClassLoader();
Thread.currentThread().setClassLoader(cl);
// now do Rome stuff
...
}
}
It’s not pretty, but it works. For now.