A Java bug

While debugging a former colleague code, a Java servlet running under TomCat, we found the following interesting bug (interesting because google is not reporting a lot of results!).

Whenever you create a URL object pointing to a normal file url (file://c:/document.xml) and try to call the openStream method, you get this very informative exception:

UnknownHostException: C

It seems that the URL object is picking the ftp transport to try and initialize the connection, and that’s where the bug is. Trying file:///c:/document.xml doesn’t work either, nor does file:///c|/document.xml. The correct solution is in fact:

file:c:/document.xml which is turned internally to file:/c:/document.xml

If anyone has an explanation on the reason of this weird behavior, I’d love to hear it!

Ads