Solved

working directory in web application

Posted on 2009-05-04
13
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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 Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to count occurrences of each item in an array.

632 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