[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

accesing config files outside jar file

hi there,

I've made a jar file which uses castor for xml processing, and the processing rules are in mapx.xml.
I've a facade like class for accessing other classes

Now i wanted to use this in a webapplication, when i run a jsp, the web server throws exception which says mapx.xml not found.

even though i can make the jar including that file, This would make furture changes difficult.


what's wrong here?
0
sudhagowshi
Asked:
sudhagowshi
  • 17
  • 11
1 Solution
 
objectsCommented:
add the config file to a subdirectory of <webapp>/WEB-INF/classes
0
 
sudhagowshiAuthor Commented:
hi objects,

I tried that , still the same problem is there
0
 
objectsCommented:
how are you referencing it?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
make sure the name corresponds to its location in the class hierarchy.
0
 
sudhagowshiAuthor Commented:
the package name say x.y.z

the xxFactory class is used to process xml, this has a reference

code is ..
 Mapping mp = new Mapping(); // Castor
mp.loadmapping("mapx.xml") // loads the rule file

that's how i reference it

0
 
objectsCommented:
and what directory is the xml file in?
0
 
sudhagowshiAuthor Commented:
i've tried puting that in all the folders of the <webapp> folder. Once i've tried it by putting those files in winnt/system32 it worked fine wonder what's wrong
0
 
objectsCommented:
it needs to be in a subdirectory (according to the naming) of your classes directory.
0
 
objectsCommented:
eg. if you xml is in <webapp>/WEB-INF/classes/config/mapx.xml
then you use something like:

mp.loadMapping(cls.getResource("/config/mapx.xml"));
0
 
sudhagowshiAuthor Commented:
what u mean by cls.getResource(); ?

could you please explain bit more?
0
 
objectsCommented:
That uses the getResource() method of Class to get the URL for the XML file.
See the javadoc for more details.

0
 
sudhagowshiAuthor Commented:
which javadoc your are refering?
0
 
objectsCommented:
For java.lang.Class
0
 
sudhagowshiAuthor Commented:
can you give me a code sample ?
0
 
objectsCommented:
i did above :)
0
 
objectsCommented:
What directory is your xml file currently in?
0
 
sudhagowshiAuthor Commented:
the class which i'm accessing through jsp is a java factory so can you explain me how to get the code done ?

right now i have it under the <webapp>
0
 
objectsCommented:
> right now i have it under the <webapp>

it needs to be under the classes directory for the classloader to see it.
also being under <webapp> makes it visible via your web server which probably isn't desirable.

0
 
sudhagowshiAuthor Commented:
ok i'll change that.

But how to load the xml file for my jar would see it ?
0
 
objectsCommented:
you need to specify a URL pointing to its location.
0
 
sudhagowshiAuthor Commented:
objects,

I'm not a pro ..

I want to get this done.

i've made a standalone which works fine with the same code. I create a jar excluding the xml mappings, to use in a web app.

And i suppose the / (root) for any webapp is that of<webapps>, then i tested with the map xml residing inside <webapp> but the server says file not find.  Please give a definite answer .
0
 
objectsCommented:
I can't give a definite answer without knowing where the xml is located :)

0
 
objectsCommented:
to get the url of the file relative to your webapp you can use the getResource() method in ServletContext class.
0
 
sudhagowshiAuthor Commented:
ic ...

xml is under
tomcat - webapps - xmlapp - rule.xml

lib is under
tomcat - webapps - xmlapp - WEB-INF - lib

jsp  is under
tomcat - webapps - xmlapp - index.jsp

hope this is ok

0
 
objectsCommented:
then you can use try:

URL url = application.getResource("/rule.xml");
0
 
sudhagowshiAuthor Commented:
ok,  

But how would my jar will know this?
0
 
objectsCommented:
you pass that url to loadMapping()
0
 
objectsCommented:
What was wrong with using getResource() to locate the xml file?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 17
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now