Monday, November 17, 2008

Which Class Loader Should I use?

Every Java developer knows thing or two about class loaders in the Java. I am sure there are plenty of articles out there to tell you more about "What class loaders are?" and their significance in the Java language. In this article I would like to go over various different ways you can get hands on a class loader, and which method I use and when and why?

There are two elements you need to know before you can load a class or resource, in a Java application
1) Name of the resource.
2) Location of the resource.

Depending upon the application, developer may or may not know above settings at design time of an application. Below we will discuss, which technique to use depending upon the situation

For example purpose let's assume that there is interface "Foo" and it has corresponding implementation class "FooImpl"

If you know the name and location, then it is pretty simple.

Foo foo = new FooImpl();

If you do not know the name, however if you *know* that resource will be reachable from the same class loader as the executing class, then you can code as below

Class fooClazz = this.getClass().getClassLoader().loadClass("FooImpl");
Foo foo = fooClazz.newInstance(); // assuming there is default constructor

or

Class fooClazz = Class.forName("FooImpl");
Foo foo = fooClazz.newInstance();

If you do not know both the name and location of resource, then it becomes little complicated. Usually this occurs when you are using some framework code, and writing some extensions and you do not have control over how your resources are being loaded (for example in application server) then the above techniques work some times, but below should be safer option


Class fooClazz = Thread.currentThread().getContextClassLoader().loadclass("FooImpl");
Foo foo = fooClazz.newInstance();


When the executing thread gets created it will be assigned class loader of the executing class, however user could be set to any other class loader at any point depending upon needs giving the most flexibility.

Sunday, November 2, 2008

Bitten By Grub, and Rescued by Super Grub

We are writing documents in DocBook format for our upcoming open source project and I needed to use Open Office 2.4 to convert some our old MS Word documents into DocBook format. Yes, now you can use Open Office Writer to save your documents in DocBook format. I was using Fedora 8 and it came with 2.3 version and it did not had easy upgrade option for 2.4 and Fedora 9 supported it. I had been putting off the Fedora 9 upgrade for a while, so, I thought this is good time as any other and popped in the Fedora 9 CD and hit upgrade and every thing went just fine.

...until I removed the CD and rebooted the machine. To my horror the the screen stopped with words

GRUB.

and would not progress any further, and my mind started racing and my hands started to tremble (after all I am a newbie at this Fedora stuff). Then I switched to another spare machine and started googling for the solution, and I came across some thread which mentioned use of Super Grub rescue disk to fix the issues with Grub, with no options at hand I was ready to give Super Grub a try.

So, I popped in the CD for Super Grub and booted the machine again. This time it booted to a menu I can't describe, the options can be be chosen by your tolerance for amount of risk you are willing to take. By now I am literally cursing.


Super Grub Disk (WITH HELP) :-)))
Super Grub Disk (NO HELP) :-|
GRUB => MBR & !LINUX! (1) AUTO ;-)))
GRUB => MBR & !LINUX! (>2) MANUAL |8-)
!LINUX! (1) AUTO
!LINUX! (>2) MANUAL
!WIN! :(((
WIN => MBR & !WIN! :(((((((((((((((((
EASY LIVE SWAP


By now couple of my colleagues who were watching me suffer (and laughing) looked at the menu and suggested I take the option with smiley face, feeling lost I hit 1, and followed directions in couple other screens and Super Grub finished it job. I rebooted again.

This time I saw the Fedora 9 booting screens and rest is history. Though I was skeptical about Super Grub based on its UI, it did it's job exactly right. So THANK YOU Super Grub team.