About Archive Tags RSS Feed


Java and The Hobbit!

26 January 2005 21:50

The Hobbit

At the start of The Hobbit Bilbo is listening to tales of the Dwarfs and he blurts out "Struck by lightening".

I've wondered about that for years - what was that about?????


Once upon a time I did a lot of Java coding, and now it seems I must do some more. The first thing I did was look at some of my old Java code.

Once upon a time I wrote a simple Java Shell.

It was a toy to learn about reflection and Java class-loaders, no job control or anything fancy.

I came across a big problem - if I loaded a class and tried to treat it as an application, invoking main when the program called exit my shell would exit too.

My solution was I think even now pretty inspired.

I added a SecurityManager and trapped calls to System.exit effectively ignoring it.

This worked wonderfully, I could load arbitary .class files, and invoke them as if they had been executed normally. But there was a new problem. My shell wouldn't ever exit itself!

What did I do? I told my new SecurityManager class to only allow the System.exit() call if the exit value was a magic constant, a number that no loaded program would ever use, but that I could tell the Shell itself to use when it wanted to terminate.

The number? It was my home phone number at the time .. I wonder how many people realised?

I wouldn't do that again ..!

| No comments