Solved

accesing config files outside jar file

Posted on 2004-09-14
30
209 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

617 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