File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Logging Problem with unpackWARs= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Logging Problem with unpackWARs="false"" Watch "Logging Problem with unpackWARs="false"" New topic

Logging Problem with unpackWARs="false"

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 221
Hello there,

Am using JDK 1.5 and Tomcat 5.5.9...

I have two init servlets for the following purposes:

1. One is for logging

2. The other is for loading (and parsing) XML based configuration files using Commons Digester.

When I go to %CATALINA_HOME%/conf/server.xml and inside the <HOST> tag, when I set the unpackWars attribute to false, I see a bunch of null values inside where my logging is supposed to be set!

If I set it to true, everything works well!

e.g. when I set unpackWars = false, redeploy my app, and then start Tomcat this is what I get:

My %CATALINA_HOME% is c:\DevTool\tomcat\jakarta-tomcat-5.5.9\

and when I deploy my application, its supposed to auto load these particular files inside %CATALINA_HOME%\myapp\WEB-INF, but since I didn't want to unpack my war files, it puts that weird nullWEB-INF inside the particular path!?

Does this mean that I can't have my servlets load init params unless my war files is always unpacked?

Here's some code for your review:

Has anyone experienced the same kind of thing?

Thanks again, JavaRanch rocks!

Happy holidays,

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Here's your problem:

The getRealPath method only works if your application is being run from an actual file system hierarchy.
If you don't unpack your war file getRealPath is supposed to return null becuase there is no real path.

From: java.lang.String&#41;" target="_blank" rel="nofollow">


public java.lang.String getRealPath(java.lang.String path)

Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

path - a String specifying a virtual path
a String specifying the real path, or null if the translation cannot be performed

[ December 21, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 221
Dear Mr. Souther,

That was such a good find (on your part)!

Thank you so much!

After looking at this article

It seems like the only three ways to do is are:

1. In %CATALINA_HOME%/conf/context.xml

set the following attribute to true by doing this:

antiJARLocking="true" inside the <Context> tag

2. Do step1 and then move a copy of your context.xml file to %CATALINA_HOME%/yourapp/META-INF/

3. Use the ClassLoader to load the XML config file and file instead of using getServletContext().getRealPath(). Can anyone explain how to use the ClassLoader to do this?

Would it be something like this:

Can anyone suggest an alternative to my getServletContext().getRealPath()?

Does anyone know of a better way than the ones listed above?


[ December 21, 2006: Message edited by: James Dekker ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410


If all you need to do is read files, look at
and ServletContext.getResourceAsStream
java.lang.String&#41;" target="_blank" rel="nofollow">

Also look at:

Lastly, Log4j will look for in the app's classpath.
I keep mine in WEB-INF/classes and it finds it.
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 221
Dear Mr. Souther:

Thank you very much for your helpful responses!

I tried my first two suggestions (of modifying the context.xml file and moving it under mywebapp/META-INF) and when I set the unpackWars attribute to false, again, and redeployed my app, this is what I got:

I'll try the ServletContext.getResource() and ServletContext().getResourceAsStream() methods next...

Happy holidays!

[ December 21, 2006: Message edited by: James Dekker ]
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 221
Dear Mr. Souther & others,

I tried using the ServletContext.getResourceAsStream() and made the following changes to my Log4jInitServlet:

I decided to test by having the war file undepacked, first.

Now, when I deploy the app (with the unpackWars = true), this is what I get in my console:

The propFile is equal the following path:


Because my build script places my in the following place:


What could I be possibly be doing wrong?

Sincerely yours,

I agree. Here's the link:
subject: Logging Problem with unpackWARs="false"
It's not a secret anymore!