Monday, January 10, 2005

Java - aaargh

Normally, I code in Python. It's easy and quick to write fairly powerful scripts, and the standard library is very extensive.

But just recently, I have had to write some Java. I've written the same code in Python, but it needed migrating into an Oracle database (don't ask why, it's a long story), which supports Java in the server, so I started converting the code.

Initially, it wasn't too hard - the main bits of the code used Python's threading library and the DB API, which aren't too dissimilar to Java's thread class and JDBC. But that fell by the wayside because Oracle's JVM serialises threads - a multithreaded program runs one thread at a time. Pah. The whole point of using threading was to run 100+ database queries in parallel! Add to that the fact that the JDBC calls were failing mysteriously, and I decided to back off from this.

So I resurrected my Python script, and decided to use Runtime.exec to fire it off and read its results. This works, and seems to be a good compromise. But I now need to transfer resultsets back from the Python program to Java. The only serious contender is to transfer the data as text on stdout, which means I have to parse text data in Java.

Boy, is that hard :-( I'd forgotten how many hoops low-level languages like Java and C++ can make you jump through just to parse text data! What do I do to split a string into words? Java 1.4 has String.split, but that's regex based, and I'm pretty sure the Oracle JVM isn't version 1.4. So does that mean I have to roll my own, with indexOf and substing, and all the error checking and boundary condition nonsense that is just built into Python's str.split()? Ack. I don't know if I can face looking at numeric conversion, string escaping, etc.

I can just feel it - this way lies XML. Why waste all that energy parsing data when there's ready-made XML libraries to do it for me? But why do I need to use something as heavyweight as XML to just transfer some tabular data?

Sigh.

I don't have any problem with Java as a language. It's just another flavour of C-like syntax, and as such has its warts, but works fine. And the library is nothing if not extensive. But things just generally seem hard. I suspect it's a mindset of the Java community. It can't be a bad thing in itself, given the success of Java. But I sure don't get it...

Oh well, back to the fiddly code. Was the index 0-based or 1-based again?

2 Comments:

Anonymous Anonymous said...

Commons Lang[1] might be worth a look. It's got some nice string handling stuff, and it should be compatable with older JVMs.

Still not as nice as Python, though. ;-)

Cheers,
Simon B,
simon@brunningonline.net,
http://www.brunningonline.net/simon/blog/

[1] http://jakarta.apache.org/commons/lang/

January 11, 2005 12:27 pm  
Blogger Paul Moore said...

Cool, thanks - I'll take a look.

In the meantime, I found StringTokenizer, which helps a little, but I'm still amazed that there isn't any method that I can find to read a number direct from an input stream...

(Oracle 9i's internal JVM is 1.3, so no regex support, either...)

January 11, 2005 1:14 pm  

Post a Comment

<< Home