Solved

accesing config files outside jar file

Posted on 2004-09-14
30
197 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
  • 17
  • 11
30 Comments
 
LVL 92

Expert Comment

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

Author Comment

by:sudhagowshi
Comment Utility
hi objects,

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

Expert Comment

by:objects
Comment Utility
how are you referencing it?
0
 
LVL 92

Expert Comment

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

Author Comment

by:sudhagowshi
Comment Utility
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
Comment Utility
and what directory is the xml file in?
0
 

Author Comment

by:sudhagowshi
Comment Utility
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
Comment Utility
it needs to be in a subdirectory (according to the naming) of your classes directory.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
what u mean by cls.getResource(); ?

could you please explain bit more?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
which javadoc your are refering?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
For java.lang.Class
0
 

Author Comment

by:sudhagowshi
Comment Utility
can you give me a code sample ?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 92

Expert Comment

by:objects
Comment Utility
i did above :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
What directory is your xml file currently in?
0
 

Author Comment

by:sudhagowshi
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
you need to specify a URL pointing to its location.
0
 

Author Comment

by:sudhagowshi
Comment Utility
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
Comment Utility
I can't give a definite answer without knowing where the xml is located :)

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
then you can use try:

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

Author Comment

by:sudhagowshi
Comment Utility
ok,  

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

Expert Comment

by:objects
Comment Utility
you pass that url to loadMapping()
0
 
LVL 92

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now