Solved

accesing config files outside jar file

Posted on 2004-09-14
30
205 Views
Last Modified: 2013-11-18
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
Comment
Question by:sudhagowshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 11
30 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12061774
add the config file to a subdirectory of <webapp>/WEB-INF/classes
0
 

Author Comment

by:sudhagowshi
ID: 12061802
hi objects,

I tried that , still the same problem is there
0
 
LVL 92

Expert Comment

by:objects
ID: 12061821
how are you referencing it?
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!

 
LVL 92

Expert Comment

by:objects
ID: 12061833
make sure the name corresponds to its location in the class hierarchy.
0
 

Author Comment

by:sudhagowshi
ID: 12061838
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
 
LVL 92

Expert Comment

by:objects
ID: 12061841
and what directory is the xml file in?
0
 

Author Comment

by:sudhagowshi
ID: 12061877
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
 
LVL 92

Expert Comment

by:objects
ID: 12061895
it needs to be in a subdirectory (according to the naming) of your classes directory.
0
 
LVL 92

Expert Comment

by:objects
ID: 12061972
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
 

Author Comment

by:sudhagowshi
ID: 12061984
what u mean by cls.getResource(); ?

could you please explain bit more?
0
 
LVL 92

Expert Comment

by:objects
ID: 12062000
That uses the getResource() method of Class to get the URL for the XML file.
See the javadoc for more details.

0
 

Author Comment

by:sudhagowshi
ID: 12062012
which javadoc your are refering?
0
 
LVL 92

Expert Comment

by:objects
ID: 12062018
For java.lang.Class
0
 

Author Comment

by:sudhagowshi
ID: 12062019
can you give me a code sample ?
0
 
LVL 92

Expert Comment

by:objects
ID: 12062026
i did above :)
0
 
LVL 92

Expert Comment

by:objects
ID: 12062032
What directory is your xml file currently in?
0
 

Author Comment

by:sudhagowshi
ID: 12062109
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
 
LVL 92

Expert Comment

by:objects
ID: 12062125
> 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
 

Author Comment

by:sudhagowshi
ID: 12062143
ok i'll change that.

But how to load the xml file for my jar would see it ?
0
 
LVL 92

Expert Comment

by:objects
ID: 12062162
you need to specify a URL pointing to its location.
0
 

Author Comment

by:sudhagowshi
ID: 12062210
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
 
LVL 92

Expert Comment

by:objects
ID: 12062267
I can't give a definite answer without knowing where the xml is located :)

0
 
LVL 92

Expert Comment

by:objects
ID: 12062306
to get the url of the file relative to your webapp you can use the getResource() method in ServletContext class.
0
 

Author Comment

by:sudhagowshi
ID: 12062310
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12062340
then you can use try:

URL url = application.getResource("/rule.xml");
0
 

Author Comment

by:sudhagowshi
ID: 12062359
ok,  

But how would my jar will know this?
0
 
LVL 92

Expert Comment

by:objects
ID: 12062396
you pass that url to loadMapping()
0
 
LVL 92

Expert Comment

by:objects
ID: 12282936
What was wrong with using getResource() to locate the xml file?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question