The moose likes Java in General and the fly likes need help troublshooting java.net.MalformedURLException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help troublshooting java.net.MalformedURLException" Watch "need help troublshooting java.net.MalformedURLException" New topic

need help troublshooting java.net.MalformedURLException

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I am just trying to load a local xml file into a TreeNode data type, the same way they do here: http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/devguide/en/html/rich_tree.html (its a little less than halfway down the page.)

So following that code, which was only snippets and not the entire code which makes me think I am missing something, here is the code I came up with:

I have tried many variations of getResourceAsStream("C:/FilePath/apache-tomcat-6.0.18/wtpwebapps/NI/test.xml") such as just getResourceAsStream("test.xml") but the same thing happens each time
I call the loadXml() and then the print() method to see the values of data and it gives me this:

Exception in thread "main" java.net.MalformedURLException
at java.net.URL.<init>(URL.java:601)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:650)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
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:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.apache.commons.digester.Digester.parse(Digester.java:1647)
at org.richfaces.component.xml.XmlTreeDataBuilder.build(XmlTreeDataBuilder.java:160)
at nodesContainer.Nodes.loadXml(Nodes.java:26)
at nodesContainer.AppNodes.main(AppNodes.java:20)

I looked at the source files and did a search on java.net.MalformedURLException but havent been able to come up with much. Any ideas?

True wisdom is in knowing you know nothing - Socrates
Rob Spoor

Joined: Oct 27, 2005
Posts: 19654

getResource() and getResourceAsStream don't accept absolute paths - they accept paths relative to the ClassLoader / Class. If you checked the result, I think you would have found that getResourceAsStream would have returned null.

How To Ask Questions How To Answer Questions
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I thought that was what I was trying to do with the print() method, check the result. It doesnt even seem to be getting that far, before it even attempts to print the value of data I get that. But I also tried getResourceAsStream("test.xml") and it gives me the same thing

Here is what the example on the RF dev guide has:

So where would they have had to put stations.xml to in order for the java code to know where to look>?
Rob Spoor

Joined: Oct 27, 2005
Posts: 19654

In the exact same folder as the .class file.

Class' getResource and getResourceAsStream can use a relative path, indicating the resource is located relative to the .class file, or an absolute path (starting with /) that indicates the resource is located relative to the class path. For instance, the following two both will try to find a file in the same path where String.class is located:
ClassLoader's getResource and getResourceAsStream are always absolute, but do not require the leading /:
jQuery in Action, 2nd edition
subject: need help troublshooting java.net.MalformedURLException
Similar Threads
Ant script not finding my build.properties file
Unable to view a simple html page in JEditorPane.
MalformedURLException with xmlParser.parse(inputSource)
Has anyone worked with Mobile Agents? (Servlet AND Mobile Agent)
Unable to parse XML