Saturday 15 June 2013

Java: chocking on unexpected BOM

I found myself needing to run a java application that reads its configuration from a properties file. This app extracts data from a database. From the configuration file it's supposed to read the url to connect to the database, username and password... amazingly, it would complain with a very cryptic error message:

ERROR: conect Database
java.sql.SQLException: No suitable driver found for
    at java.sql.DriverManager.getConnection(DriverManager.java:602)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)


What? no driver for ""... but the url is perfectly fine in the config file:
url=jdbc:oracle:thin://@localhost:1521/xe

After modifying the sources, recompiling the package, and running the new code to get the same error message... with extra debugging data I found out the source of the problem: JAVA!!!

Amazingly, if you edit the properties file in windows with the infamous notepad++, by default, it adds a useless and unnecessary BOM that makes java choke running the application or compiling the sources.

To be fair, adding a BOM to a UTF-8 encoded file is not necessary and not recommended by the standard, but I'd expect java to be more robust...