• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12021
  • Last Modified:

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
0
rahulkothari
Asked:
rahulkothari
  • 4
  • 3
  • 2
  • +3
1 Solution
 
neonlinesCommented:
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
hintCommented:
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now