rahulkothari
asked on
Read from relative path.
Hi
I have this trivial problem ... :) beleive it is major for me ... I just want to tell that to myself ....
I have a XML file in my Java project (command line based) . The XML file is in the folder <classes>/resources/db.xml .
Now I want to read this XML file.
I have tried this.getClass().getResourc esAsStream ("db.xml") ; /// bombs right on my face with malformed url exception
this.getClass().getResourc es("db.xml ").getFile (); /// i feel even worse.
Can any one hash this out for me ...
RK
I have this trivial problem ... :) beleive it is major for me ... I just want to tell that to myself ....
I have a XML file in my Java project (command line based) . The XML file is in the folder <classes>/resources/db.xml
Now I want to read this XML file.
I have tried this.getClass().getResourc
this.getClass().getResourc
Can any one hash this out for me ...
RK
Try
URL url = getClass().getResource("/r esources/d b.xml");
and pass that to your parser
URL url = getClass().getResource("/r
and pass that to your parser
assuming <classes> is in your classpath use:
InputStream in = this.getClass().getResourc esAsStream ("/resourc es/db.xml" );
Or use a relative name, being relative to the class that is making the call, eg. if class is in <classes> directory you would use:
InputStream in = this.getClass().getResourc esAsStream ("resource s/db.xml") ;
InputStream in = this.getClass().getResourc
Or use a relative name, being relative to the class that is making the call, eg. if class is in <classes> directory you would use:
InputStream in = this.getClass().getResourc
Hi rahulkothari,
Use
this.getClass().getResourc eAsStream( "resources /db.xml");
(no s at the end of Resource in getResourceAsStream)
also, the path you specify must be relative to your class directory.
Use
this.getClass().getResourc
(no s at the end of Resource in getResourceAsStream)
also, the path you specify must be relative to your class directory.
You can also
replace this.getClass()
by
<the_class_name>.class
where <the_class_name> is the name of the class (usefull when "this" is not available from static methods)
replace this.getClass()
by
<the_class_name>.class
where <the_class_name> is the name of the class (usefull when "this" is not available from static methods)
Two other objects commonly passed to parsers are:
InputSource in = new InputSource(new InputStreamReader(getClass ().getReso urceAsStre am("/resou rces/db.xm l")));
and
Reader in = new InputStreamReader(getClass ().getReso urceAsStre am("/resou rces/db.xm l"));
InputSource in = new InputSource(new InputStreamReader(getClass
and
Reader in = new InputStreamReader(getClass
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is how it worked
this.getClass().getClassLo ader().get ResourceAs Stream("re sources/db .xml");
Since the resouces folder was in classpath no in the current package so getClass().getResourceAsSt ream("/res ources/db. xml") did not work and also if we put the / then it will not work.
Thanx anyways ... for all the hints.
RK
this.getClass().getClassLo
Since the resouces folder was in classpath no in the current package so getClass().getResourceAsSt
Thanx anyways ... for all the hints.
RK
So what happens with
URL url = getClass().getResource("db .xml");
?
URL url = getClass().getResource("db
?
ASKER
well to be honest i tried it and it gave me back null so did not look into it again. But I am sure we have to get the resource from Classloader since it is this class which is aware of the entire classpath. The "this" is only aware of its current package I believe.
Let me know if you have a different point of view so that we can get more elegant solution.
RK
Let me know if you have a different point of view so that we can get more elegant solution.
RK
It calls the classloader anyway from what i can remember. It's a question of getting the path right
ASKER
oh really ... let me try again.
RK
RK
AFAIK, getResourceAsStream only checks for resources relative to the current folder.i.e. the folder where the call is made. e.g. If you run it under mypackage.MyClass, it will only check for the resource relative to "mypackage".
The getSystemResourceAsStream on the otherhand, checks the entire classpath (i.e. System) for the resource file.
The getSystemResourceAsStream on the otherhand, checks the entire classpath (i.e. System) for the resource file.
1. Give full path with the file as this.getClass().getResourc
or
2. Include <classes>/resources in ur CLASSPATH