Solved

working directory in web application

Posted on 2009-05-04
13
395 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Read about why website design really matters in today's demanding market.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

21 Experts available now in Live!

Get 1:1 Help Now