Well, it's not really day #3. But I've spent a fair amount of time on Friday and over the weekend reading a copy of "The Java Programming Language" to refresh my memory on all this new-fangled Java stuff.
I decided it was time I wrote a stand-alone Java program to do... something. Normally, you'd expect someone to write the standard "Helllo, world." program and build from there. No, not me. That's too easy and likely to work on the first try.
No, for me a good first program is one that connects to MySQL, runs a query, and spits out the results. In this case, the query was to be SHOW DATABASES. As you might guess, that asks MySQL for a list of all the databases it knows about. Think of it as "Hello, MySQL."
Once it was unpacked and installed, I began working on the code. I had found some example code on-line was close to what I needed, so I adapted it a bit. Once it compiled successfully, I spent a lot of time chasing down a really dumb run-time exception. It seemed that Java could find the driver I had just installed. According to the docs, I could either put a whole directory tree into my CLASSPATH or just use the single JAR file. I opted for the jar file, because it seems cleaner. I'd rather deal with a single file. I'm sure that's a bit of a performance hit, but it's only startup cost and I really don't care about the startup cost. I'll be writing server-side code anyway, so it generally starts once and then runs for a long time.
Anyway, to make a long story short, Sun did something rather stupid. I wasted a good hour because I should have put an explicit reference to the jar file in my $CLASSPATH. It's not sufficient to simply drop the jar file into one of the directories specified in $CLASSPATH.
At this point, I felt like the guy in that one switch parody who, after upgrading to OS X from OS 9, says "What the fuck?!"
Do the morons at Sun (one of the oldest Unix companies!) not realize how Unix path variables normally work? Paths specify directory names, not zip, tar, jar, or any other type of archive files.
Note to Sun: Thanks for breaking 20+ years of tradition so that I could waste an hour scatching my ass on this problem. There's nothing like pulling dumb tricks like this to make the barrier to entry just a little bit higher.
Why can't Java just check all the *.jar files that happen to live in given directory? Is that too damned hard?
Anyway, the good news is that after I fixed that stupid problem (I copied the jar file to $JAVA_HOME/jre/lib/ext instead of endorsing Sun's bad decision), the code worked on the first try. Granted, the code's a bit verbose compared to the Perl equivelant, but it's a nice little class that could be reused in some other simple proof-of-concept code. It all reminds me a bit of learning C++ way back when.
Excellent. My version of "Hello, MySQL." works. That wasn't so hard after all.
Next up, learning a bit more about the JDBC API, connection pooling in Java, and related fun stuff. Oh, I also want to play with Connector/J's failover feature. It sounds like a neat idea that I just might have to implement in DBIx::DWIW for Perl.
Posted by jzawodn at December 22, 2002 11:21 PM