Link to home
Start Free TrialLog in
Avatar of rahulkothari
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().getResourcesAsStream("db.xml"); /// bombs right on my face with malformed url exception
this.getClass().getResources("db.xml").getFile(); /// i feel even worse.

Can any one hash this out for me ...

RK
Avatar of Madhavan Sundarraj
Madhavan Sundarraj
Flag of Saudi Arabia image

try the following

1. Give full path with the file as this.getClass().getResources("<classes>/resources/db.xml").getFile();

or

2. Include <classes>/resources in ur CLASSPATH

Avatar of CEHJ
Try

URL url = getClass().getResource("/resources/db.xml");

and pass that to your parser
assuming <classes> is in your classpath use:

InputStream in = this.getClass().getResourcesAsStream("/resources/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().getResourcesAsStream("resources/db.xml");
Avatar of Webstorm
Webstorm

Hi rahulkothari,

Use
    this.getClass().getResourceAsStream("resources/db.xml");
(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)

Two other objects commonly passed to parsers are:

InputSource in = new InputSource(new InputStreamReader(getClass().getResourceAsStream("/resources/db.xml")));

and

Reader in = new InputStreamReader(getClass().getResourceAsStream("/resources/db.xml"));


ASKER CERTIFIED SOLUTION
Avatar of hint
hint

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rahulkothari

ASKER

This is how it worked

this.getClass().getClassLoader().getResourceAsStream("resources/db.xml");

Since the resouces folder was in classpath no in the current package so getClass().getResourceAsStream("/resources/db.xml") did not work and also if we put the / then it will not work.

Thanx anyways ... for all the hints.

RK
So what happens with

URL url = getClass().getResource("db.xml");

?
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
It calls the classloader anyway from what i can remember. It's a question of getting the path right
oh really ... let me try again.

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.