Solved

working directory in web application

Posted on 2009-05-04
13
396 Views
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.
0
Comment
Question by:Juuno
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
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

0
 

Author Comment

by:Juuno
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?
0
 
LVL 86

Expert Comment

by:CEHJ
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

http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.io.InputStream)
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:Juuno
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 86

Expert Comment

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

Expert Comment

by:CEHJ
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

http://edocs.bea.com/wls/docs61/webapp/basics.html#136976
0
 

Author Comment

by:Juuno
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
0
 
LVL 86

Expert Comment

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

Accepted Solution

by:
objects earned 250 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

http://helpdesk.objects.com.au/java/how-to-get-the-path-of-a-file-in-a-web-application



0
 
LVL 1

Assisted Solution

by:juniorDev
juniorDev earned 250 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
"/WEB-INF/classes/resources/myXML.xml."

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
or
"/mywebapp/WEB-INF/classes/resources/myXML.xml"

Try it out and see if it works for you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

17 Experts available now in Live!

Get 1:1 Help Now