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
rahulkothariAsked:
Who is Participating?
 
hintConnect With a Mentor Commented:
add resources to your class path and use ClassLoader.getSystemResourceAsStream("db.xml");

You should also be able to access db.xml as long as it is in your classpath.
0
 
neonlinesProject managerCommented:
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

0
 
CEHJCommented:
Try

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

and pass that to your parser
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
objectsCommented:
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");
0
 
WebstormCommented:
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.
0
 
WebstormCommented:
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)

0
 
CEHJCommented:
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"));


0
 
rahulkothariAuthor Commented:
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
0
 
CEHJCommented:
So what happens with

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

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

RK
0
 
hintCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.