Today morning when starting a general build of the project I'm currently working on, I got an error saying "unknown protocol: d". It was strange since I couldn't remember of any big changes made and the build didn't also fail the last time. But that's how programming is sometimes. Anyway, "d" was indicating that something must have gone wrong when accessing the hard-disk drive "D". The stack-trace gave me the following:
Exception in thread "main" java.net.MalformedURLException: unknown protocol: d
at java.net.URL.(Unknown Source)
at java.net.URL.(Unknown Source)
at java.net.URL.(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:891)
at org.devbook.utils.XMLHandler.buildDOM(XMLHandler.java:50)
at org.devbook.utils.XMLHandler.loadXMLFile(XMLHandler.java:37)
at org.devbook.data.XMLPersistencyAdapter.load(XMLPersistencyAdapter.java:159)
at org.devbook.sandbox.TestClass.main(TestClass.java:22)
It turned out that the SAXBuilder of JDom caused the error:
private void buildDOM() throws JDOMException, IOException{
SAXBuilder builder = new SAXBuilder();
try {
doc = builder.build(xmlPath);
} catch (JDOMException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
It seems as if the String "xmlPath" which is in the form "D:\\Folder1\\...\\file.xml" is not correctly interpreted as a file reference. Normally such path references are converted internally into the correct file / URL references. If this is not the case - as in my example here - one has to do it by either explicitly creating a valid URL, appending "file:///" such that it looks as follows
doc = builder.build(new URL("file:///" + xmlPath));
or to directly create a file reference. I preferred that solution:
private void buildDOM() throws JDOMException, IOException{
SAXBuilder builder = new SAXBuilder();
try {
doc = builder.build(new File(xmlPath));
} catch (JDOMException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
Attention: Just appending the protocol type "file:///" to the String "xmlPath" isn't enough. It wouldn't show any error but it would not load the xml file correctly (at it least that was my situation). To make it work correctly, one has to explicitly create an object of type java.net.URL (as shown above).
Questions? Thoughts? Hit me up
on Twitter