Locating server side config file

2 views
Skip to first unread message

mikea_59

unread,
Feb 9, 2008, 12:37:44 PM2/9/08
to Google Web Toolkit
I'm deploying to Tomcat. I have read other threads about locating
config files on the server side, like a dataSource.xml file. Others
have suggested putting the file in WEB-INF/classes directory. My
deployment does not have a WEB-INF/classes dir. I have jar'ed up all
my classes and put them in a WEB-INF/lib dir. The app runs ok under
Tomcat, just can't find the dataSources.xml file. I tried putting it
in WEB-INF and WEB-INF/lib dir. In each case I cannot find the file
from my server code.

Any suggestions?

walden

unread,
Feb 11, 2008, 12:25:55 PM2/11/08
to Google Web Toolkit
Yep, just one suggestion, because this is not a GWT question at all.

You need to put that file on the webapp classpath and refer to it by
the correct path name. You could:

1. create a WEB-INF/classes directory and throw it directly in there.
2. jar it up with the rest of your resources, but in the root folder
of that jar, and then refer to it as just "dataSources.xml" in your
code.
3. if you jar it up in a subdirectory, just make sure you reference it
properly from code, relative to the actual classpath. So if it's in
your jar as /resources/dataSources.xml, then make sure you refer to it
exactly the same in code. This would apply equally if you were to put
it in WEB-INF/classes/resources.

Hope that helps,

Walden

mikea_59

unread,
Feb 11, 2008, 6:41:25 PM2/11/08
to Google Web Toolkit

I need to make it editable for the admin to make modifications, so
I just created a local directory as you suggested and referred to it
using the appropriate path. One thing I learned, the defualt directory
for an application running under Tomcat is the Tomcat directory itself
- I would have thought it would have been the app directory.

Thanks for the advice.

On Feb 11, 11:25 am, walden <wmath...@aladdincapital.com> wrote:
> Yep, just one suggestion, because this is not a GWT question at all.
>
> You need to put that file on the webapp classpath and refer to it by
> the correct path name.  You could:
>
> 1. create a WEB-INF/classes directory and throw it directly in there.
> 2. jar it up with the rest of your resources, but in the root folder
> of that jar, and then refer to it as just "dataSources.xml" in  your
> code.
> 3. if you jar it up in a subdirectory, just make sure you reference it
> properly from code, relative to the actual classpath.  So if it's in
> your jar as /resources/dataSources.xml, then make sure you refer to it
> exactly the same in code.  This would apply equally if you were to put
> it in WEB-INF/classes/resources.
>
> Hope that helps,
>
> Walden
>
> On Feb 9, 12:37 pm,mikea_59<mikea...@yahoo.com> wrote:
>
>
>
> > I'm deploying to Tomcat. I have read other threads about locating
> > config files on the server side, like a dataSource.xml file. Others
> > have suggested putting the file in WEB-INF/classes directory. My
> > deployment does not have a WEB-INF/classes dir. I have jar'ed up all
> > my classes and put them in a WEB-INF/lib dir. The app runs ok under
> > Tomcat, just can't find the dataSources.xml file. I tried putting it
> > in WEB-INF and WEB-INF/lib dir. In each case I cannot find the file
> > from my server code.
>
> > Any suggestions?- Hide quoted text -
>
> - Show quoted text -

walden

unread,
Feb 12, 2008, 8:14:45 AM2/12/08
to Google Web Toolkit
Java doesn't exactly support the notion of a working directory, and
it's not really the "Java way" to find files the way you are doing
it. It's the classpath, strange as that sounds. In Tomcat, or any
Java web container for that matter, the classpath is rooted at WEB-INF/
classes and WEB-INF/lib/*.jar. If portability is any concern, you
should try to stick to that. Even if portability is not an issue, I
find that packaging and configuration quickly get out of hand when you
have critical files scattered around the system. I'm totally sold on
JEE packaging.

Editing a resource, if it needs to be done frequently (not just once
per release of the software), is generally a use case for persisting
the config in a database and offering a web interface to it.
Sometimes config files are edited slightly more often than just on
install, and it seems like overkill to build a whole database/web form
thing to do it. JBoss has an interesting solution for this through
their JMX console in that you can edit values that are initally
supplied in xml service descriptors, and JBoss will save your edits
back to those files.

Best of luck,
Walden
> > - Show quoted text -- Hide quoted text -

mikea_59

unread,
Feb 20, 2008, 4:57:17 PM2/20/08
to Google Web Toolkit
Walden,

This was very helpful, but I am am still having problems. I'm
working with a one time installation kind of config file so I don't
really want to mess with a database. I jar'ed up my classes and put
them in WEB-INF/lib. I build a war file and deploy it to jboss (or
Tomcat). I tried putting the config file in my WEB-INF directory and I
can't find it. I try putting it in WEB-INF/lib and still can't find
it. I don't have a WEB-INF/classes directory.

If I try to open a file on the server in Java like this:

new File("config.xml");

It always fails because it can't find the file.

If the File contstructor looks in the classpath for a file, I guess
I don't understand how a deployed application's classpath gets set for
an app that's running in a web container like jboss/tomcat, etc.

I just found this method on GenericServlet

getServletContext().getRealPath("/");

I'm going to try messing with this to see if I can get the real
path to my app.

Thanks,
Mike
> > > You need to put thatfileon the webapp classpath and refer to it by
> > > the correct path name.  You could:
>
> > > 1. create a WEB-INF/classes directory and throw it directly in there.
> > > 2. jar it up with the rest of your resources, but in the root folder
> > > of that jar, and then refer to it as just "dataSources.xml" in  your
> > > code.
> > > 3. if you jar it up in a subdirectory, just make sure you reference it
> > > properly from code, relative to the actual classpath.  So if it's in
> > > your jar as /resources/dataSources.xml, then make sure you refer to it
> > > exactly the same in code.  This would apply equally if you were to put
> > > it in WEB-INF/classes/resources.
>
> > > Hope that helps,
>
> > > Walden
>
> > > On Feb 9, 12:37 pm,mikea_59<mikea...@yahoo.com> wrote:
>
> > > > I'm deploying to Tomcat. I have read other threads about locating
> > > > config files on the server side, like a dataSource.xmlfile. Others
> > > > have suggested putting thefilein WEB-INF/classes directory. My
> > > > deployment does not have a WEB-INF/classes dir. I have jar'ed up all
> > > > my classes and put them in a WEB-INF/lib dir. The app runs ok under
> > > > Tomcat, just can't find the dataSources.xmlfile. I tried putting it

gregor

unread,
Feb 20, 2008, 9:40:50 PM2/20/08
to Google Web Toolkit
Hi Mike,

There are so many different requirements for deployment and so there
are zillions of ways to go about handling application configuration
resources. Which makes a) choosing your approach a pain and b) getting
off the ground with it a pain. I would suggest you consult the Tomcat
and JBoss forums (because this is not a GWT issue) and learn about how
they handle class loaders and provide handles to standard resource
directories. Then there's JNDI and MBeans to consider and so on. Or as
walden says, stick it in a DB.

In the short term you could locate your properties file in WEB-INF/
classes in your WAR - this means it will be on the classpath of your
deployed GWT WAR and can be accessed like so:

Properties myProps = new Properties();
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
myProps.load(classLoader.getResourceAsStream("MyProps.properties"));

or use an XML parser for XML file instead.

regards
gregor

mikea_59

unread,
Feb 21, 2008, 12:22:25 AM2/21/08
to Google Web Toolkit

I will move this to the Tomcat or jboss forums, just wanted to add
I have learned that deploying as a war is probably not a good idea in
my case because that makes it hard for user to find the config file to
edit - war gets exploded but where? Depends on servlet container.
Also, I noticed getServletContext() returns null when app is deployed
as a war. Anyway, thanks for the tips, I'll give them a try.

walden

unread,
Feb 21, 2008, 9:02:18 AM2/21/08
to Google Web Toolkit
Mike,

Gregor is right; this is off topic.

However, you're "there", so we should finish. See gregor's technique
above for loading a properties file via the classpath. This is the
technique you want to use when you configure a Java application.

As far as not having a WEB-INF/classes directory, simply create one.
Anything you put in there is findable on the classpath.

As for editing properties post-deployment, look into deploying an
"exploded war", which is just the corresponding directory structure
not archived. If you still have questions, you can email me directly
instead of staying off topic here.

Walden
Reply all
Reply to author
Forward
0 new messages