2

I am trying to implement Apache Qpid into our architecture and I am having trouble reading in a properties file.

Properties props = new Properties(); props.loadFromXML(this.getClass().getResourceAsStream("hello.properties"));

loadfromxml is throwing me an error, here is the stack trace:

> java.util.InvalidPropertiesFormatException:
> org.xml.sax.SAXParseException: Content is not allowed in prolog.  at
> java.util.XMLUtils.load(XMLUtils.java:56)     at
> java.util.Properties.loadFromXML(Properties.java:852)     at
> com.irad.message.system.HelloTest.runTest(HelloTest.java:29)  at
> com.irad.message.system.HelloTest.main(HelloTest.java:23) Caused by:
> org.xml.sax.SAXParseException: Content is not allowed in prolog.  at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
>   at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1427)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1036)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
>   at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
>   at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
>   at
> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
>   at
> com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
>   at
> com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
>   at java.util.XMLUtils.getLoadingDoc(XMLUtils.java:82)   at
> java.util.XMLUtils.load(XMLUtils.java:54)     ... 3 more

Here is the hello.properties file:

java.naming.factory.initial=org.apache.qpid.jndi.PropertiesFileInitialContextFactory
connectionfactory.qpidConnectionfactory= amqp://guest:guest@clientid/test?brokerlist='tcp://localhost:5672'
destination.topicExchange = amq.topic

What am I doing wrong? I copied the code snippet from the documentation page of Qpid.

Clocker
  • 1,316
  • 2
  • 19
  • 29
  • 1
    you need `props.load(...)` to load from regular properties file – hoaz Dec 11 '12 at 17:36
  • That throws me another exception javax.naming.NoInitialContextException because it cant instantiate the class on the file. There should be way to be able to load the file onto the props object :/ – Clocker Dec 11 '12 at 17:40
  • I think another exception does not relate to property file loading. Check your stacktrace – hoaz Dec 11 '12 at 17:41
  • 1
    I think you're right. I changed it to props.load(getClass().getResourceAsStream("hello.properties")) so now I might just be missing a jar or something. Thanks! – Clocker Dec 11 '12 at 18:07

1 Answers1

4

You are using Properties.loadFromXML to load a TEXT file as XML, and it's telling you the XML file you've given it is malformed.

Next time when you get errors like this, do a google search for 'java loadfromxml'. It will take you to the javadocs manual on how to use it and you will see that the parameters you are feeding it is incorrect:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html#loadFromXML%28java.io.InputStream%29

The above link says:

The XML document must have the following DOCTYPE declaration:

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

Change your properties file to be of correct format, and then you'll be on your way to figuring out the next error.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 1
    If I had wanted the file to be an XML file i would've put the and I wouldn't have asked this question. Its a properties file and the question is why the snippet from the documentation is wrong and how do I load the .properties file onto a properties object. – Clocker Dec 11 '12 at 17:55
  • Then why are you using the method: "loadFromXML"? the name implies you want to load an XML file. The question for the google should be something along the lines of: "How do I use java Properties to load a properties file". Of which there are thousands of answers, so no need for an additional Stackoverflow question. If you do it'll be labelled dupe and closed. Here is your answer: http://stackoverflow.com/questions/333363/loading-a-properties-file-from-java-package – Eric Leschinski Dec 11 '12 at 17:56
  • I used the loadFromXml() because thats what the documentation page used in their sample code. The answer you gave me does not work either since I had already tried that. – Clocker Dec 11 '12 at 18:03
  • The sample code you copied is wrong, often times people who create educational materials do these sorts of things on purpose, to teach you how to problem solve, which is precisely what you are doing this very minute. I was equally surprised when I learned that educational materials purposefully put errors in their examples to prevent readers from becoming mindless parrots. The other explanation is that the people who wrote the example you copied from are incompetent, which is the more likely explanation. It's to test your mettle to see if you has what it takes to be a programmer. – Eric Leschinski Dec 11 '12 at 18:06
  • Well isn't that the truth. Thank you for the insight and almost helpful code. – Clocker Dec 11 '12 at 18:09