working directory in web application

Posted on 2009-05-04
Medium Priority
Last Modified: 2012-05-07
I have my Java project, say in C:\User\Workspace\myApplication
And theres an xml file in that folder: C:\User\Workspace\myApplication\myXML.xml

And in my java program, I will take that xml file to retrieve information. So, I need to call it from my program. And now I am loading that file like:
File xmlfile = new File(C:\\User\\Workspace\\myApplication\\myXML.xml)

And I know that its not a good way to load a file using directly the absolute path. So, I try to call it from my program dynamically. Here, my program is web application. And I use Tomcat.

First, I tried this way:

File directory = new File(.);
String loc = directory.getCanonicalPath();
File xmlfile = new File(loc + myXML.xml);

And, second I tried using: System.getProperty(user.dir);

Both first and second way give me the same result. When I tried to test those methods as Java application, they both gave me the path : C: \User\Workspace\myApplication

But when I tried to put it in my program which accepts input from JSP and run on server, then the result path is: C:\Eclipse. So, theres an error that the system cannot find the file.

Why it cannot find the actual path?
And how can I do it to load that xml file into my Java program.

Any reply would be greatly appreciated.
Question by:Juuno
LVL 86

Expert Comment

ID: 24295452
You'd be better off having a resources directory under your classes folder and load it with
InputStream in = getClass().getResourceAsStream("/resources/myXML.xml");

Open in new window


Author Comment

ID: 24295619
yeah.. I ve already tried it... my classes folder is in :
C: \User\Workspace\myApplication\build\classes

so, i created a folder 'resources' , it's path is : C: \User\Workspace\myApplication\classes\resources

and I changed it to: InputStream in = getClass().getResourceAsStream("/resources/myXML.xml");

but there's an errror: java.lang.IllegalArgumentException: InputStream cannot be null

And since I used DOM and File to parse the xml file for DOM,  how can I change that InputStream into File path?
LVL 86

Expert Comment

ID: 24295685
Your classes directory should be under WEB-INF

>>And since I used DOM and File to parse the xml file for DOM,  how can I change that InputStream into File path?

Use an InputStream - that's more flexible

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.


Author Comment

ID: 24295766
But classes folder already exist under workspace\myApplication\build.
So, do i need to copy it under WEB-INF folder?
LVL 86

Expert Comment

ID: 24295842
Let's put it this way: it needs to *end up* under WEB-INF or it won't be loaded

Author Comment

ID: 24296226
But it's already exist in build, not under WEB-INF, and under WEB-INF, there's only one file: web.xml.
So, I tried to copy the whole 'classes' folder under WEB-INF. But there's still the same error and I couldn't do it.
LVL 86

Expert Comment

ID: 24296412
Sounds like your web app doesn't observe standards. In that case, just copy resources/myXML.xml under your classes folder
LVL 86

Expert Comment

ID: 24296464
The following shows what the directory structure of a web app should be (you can ignore the weblogic stuff) and your resources folder should be under classes


Author Comment

ID: 24296600
ya.. I changed my classes folder output to WEB-INF/classes in buld-path. But still have that error:
java.lang.IllegalArgumentException: InputStream cannot be null
LVL 86

Expert Comment

ID: 24296734
Please post the full current path to the xml file
LVL 92

Accepted Solution

objects earned 1000 total points
ID: 24299735
you don't need to move your xml, you can access from anywhere with your web application using the getRealPath() method



Assisted Solution

juniorDev earned 1000 total points
ID: 24824418
I think you may not have an understanding of the context-root of your web app.  The "context" of your web-app is the folder in which it is located.  This is the folder under the "webapps" folder in the tomcat directory hierarchy.

If you're calling getClass().getResourceAsStream("/resources/myXML.xml") from a JSP or servlet, then it is attempting to retrieve that file from the context-root ("/" at the beginning of the file path points to the context-root, e.g. " mywebapp/resources/myXML.xml".)  You either need a resources folder under the context-root of your app or you need to change the path to

If you're calling that code from an applet that was launched by your webapp, then you may need to format the path like this:
"/mywebapp/resources/myXML.xml" - if you have resources folder under context-root of webapp

Try it out and see if it works for you.

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Before you approach an Ionic Mobile App development company for developing mobile apps using the ionic framework, you must know why you should choose the framework. Let's try to understand that through this article.
Now many of you may wonder how you can reach great design with least number of essentials. Isn’t stuffing ‘more and more number of elements that speak for the purpose’ the right way to do it? Erm, not always, and in this write-up, you will see why.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

600 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