Juuno
asked on
working directory in web application
I have my Java project, say in C:\User\Workspace\myApplic ation
And theres an xml file in that folder: C:\User\Workspace\myApplic ation\myXM L.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\\myApp lication\\ 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\myApplicat ion
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.
And theres an xml file in that folder: C:\User\Workspace\myApplic
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\\myApp
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\myApplicat
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.
ASKER
yeah.. I ve already tried it... my classes folder is in :
C: \User\Workspace\myApplicat ion\build\ classes
so, i created a folder 'resources' , it's path is : C: \User\Workspace\myApplicat ion\classe s\resource s
and I changed it to: InputStream in = getClass().getResourceAsSt ream("/res ources/myX ML.xml");
but there's an errror: java.lang.IllegalArgumentE xception: 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?
C: \User\Workspace\myApplicat
so, i created a folder 'resources' , it's path is : C: \User\Workspace\myApplicat
and I changed it to: InputStream in = getClass().getResourceAsSt
but there's an errror: java.lang.IllegalArgumentE
And since I used DOM and File to parse the xml file for DOM, how can I change that InputStream into File path?
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)
>>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)
ASKER
But classes folder already exist under workspace\myApplication\bu ild.
So, do i need to copy it under WEB-INF folder?
So, do i need to copy it under WEB-INF folder?
Let's put it this way: it needs to *end up* under WEB-INF or it won't be loaded
ASKER
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.
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.
Sounds like your web app doesn't observe standards. In that case, just copy resources/myXML.xml under your classes folder
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
http://edocs.bea.com/wls/docs61/webapp/basics.html#136976
ASKER
ya.. I changed my classes folder output to WEB-INF/classes in buld-path. But still have that error:
java.lang.IllegalArgumentE xception: InputStream cannot be null
java.lang.IllegalArgumentE
Please post the full current path to the xml file
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window